I have this simple interface :
interface ValidationBehavior {
fun onValidated()
}
This interface is used in one function of a class :
private enum class BehaviorEnum {
IDLE,
NAVIGATEBACK
}
private fun getBehavior(payloadBehavior: String) : ValidationBehavior {
when(BehaviorEnum.valueOf(payloadBehavior)) {
BehaviorEnum.IDLE -> return object: ValidationBehavior {
override fun onValidated() {
// do some stuff
}
}
}
}
My question is : is there a way to simplify the return statement with a lambda ? I try some stuff like this but it doesn't work :
return ValidationBehavior{ () -> //do some stuff }
EDIT :
Kotlin supports SAM interfaces now so declaring it like so :
fun interface ValidationBehavior {
fun operator onValidated()
}
allows us to write :
return ValidationBehavior { //do some stuff }