Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created August 27, 2019 05:39
Show Gist options
  • Save kmizu/4d7b31747269ab08494a00efdcfa1c51 to your computer and use it in GitHub Desktop.
Save kmizu/4d7b31747269ab08494a00efdcfa1c51 to your computer and use it in GitHub Desktop.
User-defined Ternary Operators in Scala
object TernaryOperator extends App {
case class Fragment[A](condition: Boolean, thenClause: () => A) {
def >(elseClause: => A): A = if(condition) {
thenClause()
} else {
elseClause
}
}
implicit class RichBoolean(self: Boolean) {
def ?[A](thenClause: => A): Fragment[A] = Fragment(
self, () => thenClause
)
}
println((1 < 2) ? ("1 < 2") > "1 >= 2")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment