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

msink

12/04/2019, 7:54 AM
I'm lost in generic expect/actual common:
Copy code
expect class WeakReference<T>(instance: T) {
    fun get(): T?
}
jvm:
Copy code
actual typealias WeakReference<T> = java.lang.ref.WeakReference<T>
native:
Copy code
actual typealias WeakReference<T> = kotlin.native.ref.WeakReference<T>
jvm works, native fails with message "The following declaration is incompatible because upper bounds of type parameters are different: public actual typealias WeakReference<T> = WeakReference<T>" What it wants?
Well, if common chenged to
Copy code
expect class WeakReference<T : Any>(referred: T) {
    fun get(): T?
}
it works with native, but fails with jvm. And really common is not my code, it's part of Jetpack Compose Runtime.
j

JoakimForslund

12/04/2019, 8:56 AM
In my case, I did WeakReference by type by creating another typealias for your generic as "Context" and then on each platform tell what the Context should be, which in turn can be a generic. Not really sure if it fits your specific usecase tho
so essentially: WeakReference<Context> typealias Context<T> = ...
t

Thomas

12/04/2019, 9:17 AM
This is what I am using in my project. I do have the new type inference enabled so maybe that makes the difference? Common:
Copy code
expect class WeakReference<T : Any>(referred: T) {

    fun clear()

    fun get(): T?
}
JVM:
Copy code
actual typealias WeakReference<T> = java.lang.ref.WeakReference<T>
Native:
Copy code
actual typealias WeakReference<T> = kotlin.native.ref.WeakReference<T>
🙏 2