How to return from an anonymous lambda in Kotlin?
Somehow the complier doesn't allow to return inside the lambda body. Since the lambda is anonym an return@...
isn't possible here.
class Foo {
var function: (String) -> Unit = { _ -> }
init {
function = { text ->
if (text == "foo"){
// do side effects here
return
//'return' is not allowed here
//This function must return a value of type Foo
}
// do side other side effects
}
}
}
EDIT: update the example so it is clear that this question is about the return statement and not coding practices