I'm using function type to store code to be invoked on button click.
How to return from this function type
Code given below :
var SearchClickEvent: ((searchString: String) -> Unit)? = null
inputDialog!!.SearchClickEvent = Search_Click
private val Search_Click = { searchString: String ->
if(searchString.isEmpty()){
return//Error msg : return is not allowed here
//How to return from here
}
}
NOTE: I'm storing a piece of code in a variable not calling or writing any function
you need to create a label with explicit return statement in lambda, for example:
// label for lambda---v
val Search_Click = action@{ searchString: String ->
if (searchString.isEmpty()) {
return@action;
}
// do working
}
OR invert the if statement as below:
val Search_Click = { searchString: String ->
if (!searchString.isEmpty()) {
// do working
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With