Youssef Shoaib [MOD]
02/23/2022, 6:11 PMfun main() {
println((2 + 2 == 4) `?` {42} `:` {5})
}
@JvmName("questionMark")
infix fun <T> Boolean.`?`(block: () -> T): IfResult<T> = IfResult<T>(if(this) block() else ElseIndicator)
@Suppress("INVALID_CHARACTERS")
@JvmName("colon")
infix fun <T> IfResult<T>.`:`(block: () -> T): T = if(isElse) block() else result as T
object ElseIndicator
@JvmInline value class IfResult<T> internal constructor(internal val result: Any?) {
val isElse get() = result == ElseIndicator
}
(psssst, it works for bitwise operators too...)Youssef Shoaib [MOD]
02/23/2022, 6:12 PMYoussef Shoaib [MOD]
02/23/2022, 6:13 PMfirst < other
should return a custom object and not a booleanlangara
02/23/2022, 6:21 PMYoussef Shoaib [MOD]
02/23/2022, 6:48 PMYoussef Shoaib [MOD]
02/23/2022, 6:50 PMMatthew Gast
02/24/2022, 4:40 AM(psssst, it works for bitwise operators too...)Yes but no. Part of the push for bitwise operators means actually following the order of operations for operators (removing the excessive need for parenthesis in some circumstances). This would be equivalent to using the existing and/or infix functions.
Rob Elliot
02/24/2022, 9:03 AM