SwiftKotlin
SwiftKotlin copied to clipboard
Switch cases with tuple decomposition values does not translate properly
This Swift code:
switch exception {
case .qrCode(let name):
trackCode(name: name)
default:
trackError(name: "generic")
}
Translates to right now to:
when (exception) {
.qrCode -> trackCode(name = name)
else -> trackError(name = "generic")
}
However, the name
variable in Swift has been decomposed from the tuple, and hence the Kotlin version should prefix its usage with the variable in the when
expression. In this case:
when (exception) {
.qrCode -> trackCode(name = exception.name)
else -> trackError(name = "generic")
}
An easier way to also get a proper translation is to use with
. This will work as long as the tuple variables keep the same definition name:
How do you use "with" with enums? (Was there more at the end of what you wrote?)
tor
On 18 Feb 2019, at 09:36, Angel G. Olloqui [email protected] wrote:
An easier way to also get a proper translation is to use with. This will work as long as the tuple variables keep the same definition name:
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/angelolloqui/SwiftKotlin/issues/58#issuecomment-464636720, or mute the thread https://github.com/notifications/unsubscribe-auth/AB-irhiJjt-Mbu1ytlQRspuJEVtR49Frks5vOmYmgaJpZM4Q5ZEw.
Yes! sorry, I should provide an example. The same code before, could be instead:
with(exception) {
when (this) {
.qrCode -> trackCode(name = name)
else -> trackError(name = "generic")
}
}
This way, the usage of name
will be actually the same than exception.name
Note that the .qrCode
should be prefixed like Exception.qrCode
or whatever, but that is a different issue.
Ah, sorry, there was an extra post with code I didn’t see in the emails I get when we message in the pull request.
Still not sure how the the "with" helps initializing from a raw value. How about we add something like this to simple enums with specific set values:
companion object { fun fromRaw (value: Int) = Types.values().find { it.value == value } }
Or am I missing something?
No, this issue is about the tuple decomposition. I have created another one with the Enum initialization here: #94