Created
July 12, 2021 15:35
-
-
Save trevorhackman/a6855da0d1a93fa696b92ca202c3a4ee to your computer and use it in GitHub Desktop.
Kotlin Ternary Operator with higher-order overloads for lazy evaluation of arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline infix fun <T> Boolean.T(trueOutput: T) = TernaryPart(this, trueOutput) | |
inline infix fun <T> TernaryPart<T>.F(falseOutput: T) = if (condition) trueOutput else falseOutput | |
class TernaryPart<T>(val condition: Boolean, val trueOutput: T) | |
// T overload for conditional evaluation of functional arguments | |
infix fun <T> Boolean.T(trueBranch: () -> T) = TernaryPart(this, trueBranch) | |
// F overload for conditional evaluation of functional arguments | |
inline infix fun <T, U: () -> T> TernaryPart<U>.F(falseBranch: U) = if (condition) trueOutput() else falseBranch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment