https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

Kurt Renzo Acosta

03/10/2020, 4:09 PM
Is it possible to have an expect/actual with different types?
Copy code
// Common
expect class MyClass {
    ...
    expect val myVal: MyCommonType
}

// Target 1
actual class MyClass {
    ...
    actual val myVal: MyTargetOneType
}

// Target 2
actual class MyClass {
    actual val myVal: MyTargetTwoType
}
Currently, I just add them but not as expect/actual variables but I can't use them in common
r

russhwolf

03/10/2020, 4:11 PM
No, the declarations need to match exactly. You could do
actual val myVal: MyCommonTime = MyTargetOneType()
but you can only interact with it in common as
MyCommonType
k

Kurt Renzo Acosta

03/10/2020, 4:12 PM
I tried using typealias but the problem is on Android, it's an abstract class and on iOS, it's an interface.
r

russhwolf

03/10/2020, 4:13 PM
Yeah you won't be able to typealias both at the same time
i

ilya.gorbunov

03/10/2020, 4:24 PM
I think you can declare an expect type for
MyCommonType
and actualize it with type aliases to
MyTargetOneType
and
MyTargetTwoType
in those targets respectively.
r

russhwolf

03/10/2020, 4:27 PM
How's that? Would need to be either
expect class
or
expect interface
. Can't be both at the same time
Unless you mean something different by "expect type" that I'm not aware of
i

ilya.gorbunov

03/10/2020, 4:45 PM
but the problem is on Android, it's an abstract class and on iOS, it's an interface.
Yes, currently we have such a limitation, but also have a plan to lift it 🙂
🎉 4
r

russhwolf

03/10/2020, 5:04 PM
Ah good to know. Looking forward to it!
l

Luoqiaoyou

03/11/2020, 2:39 PM
// common expect class Test // jvm actual typealias Test = JVMTest // ios actual typealias Test = IOSTest
s

saket

04/26/2020, 7:34 AM
Finding this thread a bit late, but I had filed a feature request for this a while ago: https://youtrack.jetbrains.com/issue/KT-33395
11 Views