This is a question about Swift mapping of a Kotlin...
# multiplatform
s
This is a question about Swift mapping of a Kotlin list to a KotlinArray. I'm asking this as a learning opportunity exploring the list/array mappings between Kotlin and Swift, not something that I'd do in real life 🙂 . Given a Kotlin static library included in an Xcode project as a cocoapod framework, lets say there is some Kotlin code in the library like this:
Copy code
sealed class Seal<T>() {
    class SealString: Seal<String> {
        ...
    }
    class SealInt: Seal<Int> {
        ...
    }
    class SealSomeClass: Seal<SomeClass> {
        ...
    }
 }
 class SealValues(vararg args: Seal<out Any>) {
    ... }
This works fine in Kotlin of course, but in Swift the vararg gets mapped to:
KotlinArray<Seal<AnyObject>>
. Also in this example, SealString gets mapped to
Seal<NSString>
. So making an instance of
KotlinArray<Seal<AnyObject>>
and attempting to add one SealString instance won't compile. NSString is an Any (a value type), not an AnyObject (a reference type), so attempting in Swift to add a SealString instance to a KotlinArray<Seal<AnyObject>> gets a syntax error:
Cannot convert expression of type 'Seal<NSString>' to type 'Seal<AnyObject>'
. I don't see a way in Swift to get around this, but I'm a Swift newbie and also new to the Kotlin/Swift mappings. Is there a way around this? If I had instances of SealSomeClass above, seems like the mapping would work since SomeClass would be a reference type and thus a flavor of AnyObject. But if given Seal<T> where T is a value type like String, Int, Double, etc., seems like this mapping can't work. I've seen YouTrack issues about improving the KotlinArray mapping set up, but haven't found an issue involving the extra complication of sealed classes using a generic. So thought I'd ask here in case anyone is interested in contributing to my learning exercise :-). Thanks in advance for any discussion...