Can someone point me to a good example of expect/a...
# kotlin-native
a
Can someone point me to a good example of expect/actual typealiasing? Seems all examples I find either have all common code or very trivial (non-custom model) expect/actual usages
I have complex classes I want to share between platforms but for ... reasons need to use the Java and Swift versions via expect/actual
k
typealiasing by definition is trivial
it sets one type equal to another
a
yeah I guess I meant more an example of the package/file structure required to achieve the same
or perhaps more usually, some usage of actual classes that are less trivial than a typealias
s
Common
Copy code
expect fun someFun(someArg: TypeA, someOtherArg: TypeB): ReturnType
iOS or Android or whatever platform(s) you want to support:
Copy code
actual fun someFun(someArg: TypeA, someOtherArg: TypeB): ReturnType {
    // implementation
}
For example I have a project structured like this:
Copy code
src
    commonMain
        package
            Socket.kt // expect
    iosMain
        package
            Socket.kt // actual implementation
    jvmMain
        package
            Socket.kt // actual implementation
And if you want your whole class to be defined per platform you can just use `actual`/`expect` on the class definition itself and not on individual methods, e.g.,
Copy code
expect class A {
    fun foo(a: Int)
    fun bar(): Boolean
}
Copy code
actual class A {
    fun foo(a: Int) {
        print(a)
    }
    fun bar(a: Int): Boolean {
        return false
    }
}
a
👍 thanks! I am having some issues with this setup and the IDE not thinking I've defined an actual but maybe it's just my own issue
k
for ios you may need to define it twice (sim/arm)