Dipendra Singh
11/22/2021, 3:46 PMval createEmail = {
if (type == Type.ERROR) {
dataProvider.createErrorEmail(id)
}
}
Nolan
11/22/2021, 4:14 PMwhen
statement.
val createEmail = when (type) {
Type.ERROR -> dataProvider.createErrorEmail(id)
}
https://kotlinlang.org/docs/control-flow.html#when-expressionRuckus
11/22/2021, 4:17 PM() -> Unit
, not Unit
.() -> CreateEmail
, you need to provide an else branch:
val createEmail = {
if (type == Type.ERROR) {
dataProvider.createErrorEmail(id)
} else {
// some other CreateEmail
}
}
or use a when statement similar to what @Nolan suggested, but wrapped in braces:
val createEmail = {
when (type) {
Type.ERROR -> dataProvider.createErrorEmail(id)
// Other branches
}
}
You can also specify the type explicitly to get a compiler error and avoid type inference altogether:
val createEmail: () -> CreateEmail = ...
Stephan Schroeder
11/22/2021, 8:49 PM