Youssef Shoaib [MOD]
12/05/2022, 1:12 AM<h2>
tag that talks about Union types, and print out it and its siblings, and this is what it produced:
See ๐งต for some other interesting observations about it, including Kotlin-specific ones, if anyone is curiousYoussef Shoaib [MOD]
12/05/2022, 1:17 AMYoussef Shoaib [MOD]
12/05/2022, 1:19 AMYoussef Shoaib [MOD]
12/05/2022, 1:21 AMYoussef Shoaib [MOD]
12/05/2022, 1:22 AMYoussef Shoaib [MOD]
12/05/2022, 1:27 AMYoussef Shoaib [MOD]
12/05/2022, 1:27 AMimport io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.http.HttpMethod
import kotlinx.html.*
import kotlinx.html.parser.parse
fun main() {
// Create an instance of the HttpClient class
val client = HttpClient()
// Make a GET request to the specified URL
val response = client.request<HttpResponse>(
method = HttpMethod.Get,
host = "<http://kotlinlang.org|kotlinlang.org>",
path = "/docs/whatsnew19.html"
)
// Parse the response data as HTML
val responseData = response.readText()
val doc = parse(responseData)
// Find the <h2> element that talks about Union Types
val h2 = doc.body?.getElementsByTag("h2")?.find { h2 ->
h2.text.contains("Union Types")
}
// Print the contents of the <h2> element and its siblings to the console
h2?.let { h2 ->
println("<h2>: " + h2.text)
h2.nextSibling?.let { sibling ->
do {
if (sibling is Element) {
println("<${sibling.tagName}>: " + sibling.text)
}
sibling.nextSibling?.let { nextSibling ->
sibling = nextSibling
}
} while (sibling.nextSibling != null)
}
}
// Close the HttpClient instance when you are finished making requests
client.close()
}
Youssef Shoaib [MOD]
12/05/2022, 2:44 AMThe client is created outside of theloop because it is more efficient to create the client once and reuse it for each iteration of the loop. Creating a new instance of thefor
class for each iteration of the loop would be wasteful and would slow down the code.HttpClient
Additionally, theclass is designed to be used as a singleton, which means that it should only be created once and then shared among different parts of the code. Creating multiple instances of theHttpClient
class can cause issues with memory management and can lead to unexpected behavior.HttpClient
In general, it is best practice to create theinstance once and then reuse it for each request that you need to make. This will help to improve the performance and efficiency of your code, and will make it easier to manage and maintain.HttpClient
Youssef Shoaib [MOD]
12/05/2022, 2:51 AMKotlin 1.9 introduces support for Union Types, a new type system feature that allows you to define a type that can either be one of several other types. This is useful in situations where you want to express that a value can have multiple different types, but you don't want to use type hierarchies or interfaces.
Union Types are similar to TypeScript's type unions, and they are represented in Kotlin using theoperator. For example, you could define a type that can either be a|
or anString
like this:Int
val foo: String | Int
You can then assign a value of typeorString
to theInt
variable, and the compiler will check that the value is one of the allowed types.foo
Union Types are covariant, which means that if you have a Union Type that is a subtype of another Union Type, then the subtype can be used wherever the supertype is expected. For example, if you define a typethat is a subtype ofFoo
, then you can useBar
whereverFoo
is expected.Bar
Union Types are also nullable, which means that if you define a type that is nullable, then it can either be the specified type or. For example, if you define a typenull
that is a Union Type ofFoo?
andFoo
, then you can usenull
whereverFoo?
is expected, and you can also assignFoo
to a variable of typenull
.Foo?
Union Types are a powerful and flexible feature that can help you to express complex type relationships in your code. They are especially useful in situations where you want to express that a value can have multiple different types, but you don't want to use type hierarchies or interfaces.
Youssef Shoaib [MOD]
12/05/2022, 2:52 AMIn Kotlin 1.10, Union Types are supported in the type system and the language. This means that you can define Union Types using theoperator, and the compiler will check that the values you use are one of the allowed types. You can also use Union Types in type declarations, function signatures, and other places in your code where you need to specify a type.|
For example, you could define a function that accepts a value of typeorString
, and returns a value of typeInt
orString
, like this:Boolean
fun foo(x: String | Int): String | Boolean {
if (x is String) {
return x.length > 10
} else {
return x.toString()
}
}
In this example, thefunction accepts a value of typefoo
orString
, and returns a value of typeInt
orString
. The function checks the type of theBoolean
parameter, and then returns a value of the appropriate type.x
You can also use Union Types in type declarations, like this:
typealias Foo = String | Int
In this example, thetype alias is defined to be a Union Type ofFoo
andString
. This means that you can use theInt
type wherever you would useFoo
orString
, and the compiler will check that the values you use are one of the allowed types.Int
Union Types are a powerful and flexible feature that can help you to express complex type relationships in your code. They are especially useful in situations where you want to express that a value can have multiple different types, but you don't want to use type hierarchies or interfaces.
vngantk
12/06/2022, 10:42 PM