Yhcs Ami
01/10/2025, 3:52 AM/**
* You can edit, run, and share this code.
* <http://play.kotlinlang.org|play.kotlinlang.org>
*/
val trick = {
println("No treats!")
}
val treat: () -> Unit = {
println("Have a treat!")
}
fun trickOrTreat(isTrick: Boolean, extraTreat: ((Int) -> String)?): () -> Unit {
if (isTrick) {
return trick
} else {
if (extraTreat != null) {
println(extraTreat(5))
}
return treat
}
}
fun main() {
val treatFunction = trickOrTreat(false) { "$it quarters" }
val trickFunction = trickOrTreat(true, null)
repeat(4) {
//trickFunction()
treatFunction()
}
//treatFunction()
trickFunction()
The execution result is as follows: