I got a switch in Swift on a Kotlin enum defined i...
# multiplatform
s
I got a switch in Swift on a Kotlin enum defined in
commonMain
and XCode complains that it's not exhaustive - but I covered every case. Is this a known bug in Kotlin/Native ?
yes black 2
s
Kotlin enums don’t map directly to Swift or ObjC enums so it probably wants you to add a default case. You can just put a break under it and it'll work.
s
Yes, I needed to add that. But what case is Swift internally missing?
l
The case where the API evolves and further enum cases are added.
s
Very funny, Louis... No, seriously: I have a "case" for every enum thing and I want no "default"-case. Default cases are bad because if I add more items in my enum I want that switch to not compile. Which enum case is Swift internally missing so that it says my enum is not exhaustive?
l
It was not a joke. That's Swift's way of dealing with enums, at least when they're not defined in Swift.
r
It's not a missing case. Swift just doesn't understand that Kotlin enums are exhaustive.
l
You can crash for the default case, so at least, you notice it at runtime.
s
So Swift is actually missing Kotlins feature to see that it's exhaustive and only wanting a "else" if some case is really missing?
Because there are Swift samples that don't have a default case.
Ok, as Sam and Russel said maybe that's Kotlin/Native problem that Kotlin enums are not directly mapped to Swift enums.
I hope that will change one day.
Thank you all.
g
You may want to track KT-49521 for direct interop with Swift, maybe it'll allow some improvements on enums too.
👍 1
s
The issue is that Kotlin enums are mapped through as ObjC classes because they can have properties and behavior. That means that Swift wants you to have a case for all possible classes when using it as a switch value. If you really want to avoid a default case, you’ll have to transform it into a Swift enum. That just seems to be more trouble than it is worth because you’ll have to remember to keep two values in sync. It’s easier just to add:
Copy code
default:
    fatalerror("You need to handle another enum value")
🙏 1
a
I wonder if it would be possible to use something like moko-kswift to automatically generate Swift-equivalents for KMP enums to make this a bit easier...
b
@Stefan Oltmann This is what we use https://github.com/icerockdev/moko-kswift you should give it shot, it's a bit of a pain to setup but you only need to do it once
👍 1