Skip to content

Instantly share code, notes, and snippets.

@gacordeiro
Last active March 13, 2020 17:17
Show Gist options
  • Save gacordeiro/05ae0a8566e070915b072b4f7fe1f021 to your computer and use it in GitHub Desktop.
Save gacordeiro/05ae0a8566e070915b072b4f7fe1f021 to your computer and use it in GitHub Desktop.
Ternary in Kotlin
class _Ternary<out T>(val condition: Boolean, val ifTrue: T)
infix fun <T> Boolean.then(ifTrue: T): _Ternary<T> = _Ternary(this, ifTrue)
infix fun <T> _Ternary<T>.otherwise(ifFalse: T): T = if (condition) ifTrue else ifFalse
const val tellMeTheTruth: Boolean = true
val result: String = tellMeTheTruth then "I'll believe you" otherwise "I'll never trust you"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment