Hmm, im surprised that this is allowed. ```// comm...
# multiplatform
x
Hmm, im surprised that this is allowed.
Copy code
// commonMain
expect class Pager

// androidMain
actual typealias Pager = PagerState
Feels weird that you can expect a class and have it actualised by a type alias
a
Perhaps my explanation of how expect/actual works might help you understand that it is not. 1. Typealias by its definition creates no new types. It is just a syntax sugar that declares a new name for some type expression. 2. Think about expects as some kind of Forward declaration . But Kotlin makes it stricter to ensure 1-to-1 relation between expects and actuals during compilation time. Basically expect declaration is a prototype of some Kotlin class or function. And compiler verifies that all usages of that prototype is possible when compiling a platform classes/binaries. Lets look at your example:
Copy code
// commonMain.kt
expect class Pager

fun foo(pager: Pager) { println(pager) }

// androidMain.kt
actual typealias Pager = PagerState
When compilation for android is requested through
./gradlew build
for example. Kotlin compiler will receive as input both source files. roughly like this:
kotlinc src/commonMain.kt src/androidMain.kt
Compiler will scan all files and will try to match all expects and actuals. And if its all right, it internally makes your code look like this:
Copy code
// internal representation
fun foo(pager: Pager) { println(pager) }
typealias Pager = PagerState
this is perfectly valid code. Hope this helps you to understand the logic behind expect/actual.
👍 1
x
that actually makes sense. Thanks for the explanation @Anton Lakotka [JB]