aleksey.tomin
12/23/2020, 4:28 PMmemScoped {
if (...) {
value?.let { callVoidFunction1() }
} else if (..) {
callVoidFunction1()
}
}
I have error on IDEA: 'if' must have both main and 'else' branches if used as an expression
I can fix it by add } else {
at end but it looks like ugly.Russell Stewart
12/23/2020, 4:29 PMif/then
statement. Either way, it's definitely non-intuitive.
One suggestion: instead of an empty else
statement, you could do this:
memScoped {
if (...) {
value?.let { callVoidFunction1() }
} else {
if (..) {
callVoidFunction1()
}
}
}
Still a little clumsy, but not quite as ugly as having an empty else
hanging around.ephemient
12/23/2020, 5:03 PMif
, so that it's not being used like a value
memScoped {
if (...) { ... }
Unit
}
rharter
12/23/2020, 5:58 PMmemScoped
has a return value and lamdas that return values automatically treat the last expression as a return, so the if
is being treated as an expression (like val foo = if ...
). As @ephemient mentioned, you could add anything after to be treated as the return instead, or put the conditional in a run
block, or anything else that makes it clear that the if is not an expression to be returned.Animesh Sahu
12/24/2020, 5:14 AMmemScoped<Unit> {