Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of actual keyword in Kotlin

Tags:

kotlin

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?

like image 848
Andrey Danilov Avatar asked Feb 14 '18 19:02

Andrey Danilov


People also ask

What is actual keyword in Kotlin?

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.

How do I use this keyword in Kotlin?

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.

What does -> mean in Kotlin?

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 }

What is the purpose of let in Kotlin?

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.


2 Answers

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.

like image 191
zsmb13 Avatar answered Oct 28 '22 11:10

zsmb13


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.

enter image description here

The compiler ensures that every declaration marked with the expect keyword in the common module has the corresponding declarations marked with the actual 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.

like image 35
codingjeremy Avatar answered Oct 28 '22 09:10

codingjeremy