SwiftKotlin icon indicating copy to clipboard operation
SwiftKotlin copied to clipboard

Switch cases with where clauses not translated properly

Open angelolloqui opened this issue 7 years ago • 0 comments

In Swift, switch cases can contain where clauses like the following:

switch exception {
case .generic(let error) where error != nil:
    trackError(name: "generic", message: error!)
default:
    trackError(name: "generic", message: R.string.localizable.generic_error())
}

Currently, SwiftKotlin generates:

when (exception) {
    .generic where error != null -> trackError(name = "generic", message = error!!)
    else -> trackError(name = "generic", message = R.string.localizable.generic_error())
}

But that is incorrect Kotlin. It should instead remove the variable from the when and use expressions for the cases. Like:

when {
    exception is Exception.generic && error != null -> trackError(name = "generic", message = error!!)
    else -> trackError(name = "generic", message = R.string.localizable.generic_error())
}

angelolloqui avatar Dec 05 '17 11:12 angelolloqui