y
02/12/2023, 12:06 PMfun returnsUnit() = if (condition) { doSomething }
…does not compile, but
fun returnsUnit() = if (condition) { doSomething } else { }
does. I understand why it is like this, but is this considered a bug?Adam S
02/12/2023, 12:07 PMIf you’re usingas an expression, for example, for returning its value or assigning it to a variable, theif
branch is mandatory.else
y
02/12/2023, 12:11 PMUnit
return values (by implicitly adding a else { return Unit }
)Adam S
02/12/2023, 12:11 PMAdam S
02/12/2023, 12:11 PMy
02/12/2023, 12:12 PMAdam S
02/12/2023, 12:14 PMelse {}
isn’t necessary if the function has a block body
fun returnsUnit() {
if (condition) {
doSomething
}
}
ephemient
02/12/2023, 12:50 PMStephan Schröder
02/12/2023, 4:21 PMreturn Unit
implicitly added to it anyway.
At least that is my mental model which seems to comply with actual behaviour (compiler implementation may differ).ephemient
02/12/2023, 4:32 PMwhen
are also not expressionsy
02/12/2023, 4:52 PMwhen
is allowed by the compiler.ephemient
02/14/2023, 7:52 AM