Is it possible to have an expect/actual with diffe...
# multiplatform
k
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
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
I tried using typealias but the problem is on Android, it's an abstract class and on iOS, it's an interface.
r
Yeah you won't be able to typealias both at the same time
i
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
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
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
Ah good to know. Looking forward to it!
l
// common expect class Test // jvm actual typealias Test = JVMTest // ios actual typealias Test = IOSTest
s
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