Is it possible to use generic types in iOS app ret...
# kotlin-native
r
Is it possible to use generic types in iOS app returned from Common module function?
s
They can be used but they’re type erased.
r
I can’t imagine what you mean
@Sam is something like this possible ?
Copy code
// common module
sealed class Option<out T>
data class Some<T>(val value: T): Option<T>()
object None : Option<Nothing>()

fun <T> T?.toOption(): Option<T> = this?.let { Some(it) } ?: None

fun getOption(): Option<String> {
    return "OK".toOption()
}

// iOS app

func get() {
    let stringValue = getOption()
    if let stringValue as? Some { stringValue.value }
        else { print("value not present") }
}
s
All of your T’s will become Object when going from Kotlin to Obj-C. Currently Obj-C supports generics but in a very limited fashion. Only the collection types can be parameterized and it works like Java where it is only a compile time check.
👍 1