From the course: Kotlin Multiplatform Development

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Expect and actual

Expect and actual

- [Instructor] So far we've used a template to modify existing code to work on multiple platforms. However, we haven't really explained how Kotlin Multiplatform works. Under the hood, there are two special Kotlin language modifiers, which were added to the core Kotlin language back when KMP was added to Kotlin in 2017. These are expect and actual. When we write expect in Kotlin, we're telling the compiler that we want to create an interface. However, unlike interfaces in most languages, a Kotlin expect interface is expected to have an actual implementation for each platform. When we write actual in Kotlin, we're telling the compiler that this is the actual implementation of the interface we told the compiler to expect earlier. The compiler can then look at the project directory structure to determine which platform this actual implementation serves. Now, as I said, expect and actual both create and satisfy an interface. However, they're also more powerful than interfaces or protocols…

Contents