I noticed that some functions for coroutines are marked with actual
keyword.
From documentation:
actual denotes a platform-specific implementation in multiplatform projects
As I understood from documentation actual
keyword is used for multiplatform projects and should work in pair with expect
keyword.
Something like this:
Common module:
package org.jetbrains.foo
expect class Foo(bar: String) {
fun frob()
}
fun main(args: Array<String>) {
Foo("Hello").frob()
}
Corresponding module:
package org.jetbrains.foo
actual class Foo actual constructor(val bar: String) {
actual fun frob() {
println("Frobbing the $bar")
}
}
That case is clear.
But in package kotlinx.coroutines.experimental
I noticed that some functions like launch
or withContext
are marked as actual
but there are no expect
functions in package.
So what is the purpose of actual
keyword without expect
?
actual denotes a platform-specific implementation in multiplatform projects. As I understood from documentation actual keyword is used for multiplatform projects and should work in pair with expect keyword.
In Kotlin, the “this” keyword allows us to refer to the instance of a class whose function we happen to be running. Additionally, there are other ways in which “this” expressions come in handy.
The -> is a separator. It is special symbol used to separate code with different purposes. It can be used to: Separate the parameters and body of a lambda expression val sum = { x: Int, y: Int -> x + y }
let is often used for executing a code block only with non-null values. To perform actions on a non-null object, use the safe call operator ?. on it and call let with the actions in its lambda. Another case for using let is introducing local variables with a limited scope for improving code readability.
The kotlinx.coroutines library actually makes use of multiplatform projects since it supports both the JVM and JS compilation targets.
You can find the common module here, and the specific expect
declarations for the functions you've mentioned here.
While the source code in the other answer helped, I found this page (linked off of the page @jim-andreas mentioned in the comments above) was much more helpful.
Specifically, this passage:
If you're developing a multiplatform application that needs to access platform-specific APIs that implement the required functionality (for example, generating a UUID), use the Kotlin mechanism of expected and actual declarations.
With this mechanism, a common source set defines an expected declaration, and platform source sets must provide the actual declaration that corresponds to the expected declaration. This works for most Kotlin declarations, such as functions, classes, interfaces, enumerations, properties, and annotations.
The compiler ensures that every declaration marked with the
expect
keyword in the common module has the corresponding declarations marked with theactual
keyword in all platform modules. The IDE provides tools that help you create the missing actual declarations.
Again, for more information, you can visit this page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With