https://kotlinlang.org logo
#touchlab-tools
Title
# touchlab-tools
c

curioustechizen

12/04/2023, 11:01 AM
Are there any recommendations around making it easier to consume a sealed hierarchy on the Swift side if I don't want to switch over the generated Swift enum? My usecase is something like this. I have the following Kotlin code:
Copy code
sealed class Action {
    object OnCtaClick : Action()
    data class OnClassClick(val classId: Int) : Action()
}

fun processAction(action: Action) {
    when(action) {
        is OnCtaClick -> {
            println("OnCtaClick")
        }
        is Action.OnClassClick -> {
            println("OnClassClick ${action.classId}")
        }
    }
}
And when I call it from Swift, I cannot take advantage of the
onEnum(of:)
facility of SKIE. I still need to call it like this:
Copy code
processAction(action: Action.OnCtaClick())
processAction(action: Action.OnClassClick(classId: 2))
But I would like to call it like this:
Copy code
processAction(.onCtaClick)
processAction(.onClassClick(classId: 2))
f

Filip Dolník

12/04/2023, 11:39 AM
Hi! SKIE doesn’t currently support this usecase. You could write your own extension function that would accept the enum generated by SKIE as the parameter and convert it to the original Kotlin class, but I’m not sure if the amount of work needed to do all of that manually is worth the improved call site syntax.
c

curioustechizen

12/04/2023, 11:50 AM
Hmm in my case the Action is actually a generic (
class BaseViewModel<S, A>
and I don't see a way to define this extension function to work with generics so I'm going to leave it as-is. The effort definitely outweighs the benefit. Thanks for the recommendation!
f

Filip Dolník

12/04/2023, 11:51 AM
yeah and I also realized that you wouldn’t be able to use the generated enum because its cases contain the original Kotlin class as associated type, so you would have to write your own
👍🏽 1