Created
August 27, 2019 05:39
-
-
Save kmizu/4d7b31747269ab08494a00efdcfa1c51 to your computer and use it in GitHub Desktop.
User-defined Ternary Operators in Scala
This file contains hidden or 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
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