Uberunix
08/10/2025, 12:48 AMfun trickOrTreat(isTrick: Boolean, extraTreat: ((Int) -> String)?): () -> Unit {
if (isTrick) {
return trick
} else {
if (extraTreat != null) {
println(extraTreat(5))
}
return treat
}
}
Is this actually common or good practice?Youssef Shoaib [MOD]
08/10/2025, 12:50 AMinline
ing, for one, which is annoying. Having a default lambda is probably the better option. joinToString
does the same thing that you do there, and it's very annoyingUberunix
08/10/2025, 12:55 AMYoussef Shoaib [MOD]
08/10/2025, 12:58 AMfun trickOrTreat(isTrick: Boolean): () -> Unit = if (isTrick) trick else treat
fun trickOrTreat(isTrick: Boolean, extraTreat: (Int) -> String): () -> Unit = if (isTrick) trick else {
println(extraTreat(5))
treat
}
Uberunix
08/10/2025, 1:02 AMUberunix
08/10/2025, 1:06 AMYoussef Shoaib [MOD]
08/10/2025, 1:10 AMUberunix
08/10/2025, 1:12 AMNicolai C.
08/10/2025, 1:12 AMloke
08/10/2025, 8:49 AMfun trickOrTreat(extraTreat: (Int)->String = {trick}): String {
return extraTreat(1234)
}
Then you can either call it without arguments, thus getting the default: trickOrTreat()
, or you cal call it with a function, to get that result: trickOrTreat { "abc" }