Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch cases with tuple decomposition values does not translate properly #58

Open
angelolloqui opened this issue Dec 7, 2017 · 5 comments
Labels

Comments

@angelolloqui
Copy link
Owner

angelolloqui commented Dec 7, 2017

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")
}
@angelolloqui
Copy link
Owner Author

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:

@torlangballe
Copy link
Collaborator

torlangballe commented Feb 18, 2019 via email

@angelolloqui
Copy link
Owner Author

angelolloqui commented Feb 18, 2019

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.

@torlangballe
Copy link
Collaborator

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?

@angelolloqui
Copy link
Owner Author

No, this issue is about the tuple decomposition. I have created another one with the Enum initialization here: #94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants