Created
December 16, 2012 04:36
-
-
Save anonymous/4303354 to your computer and use it in GitHub Desktop.
Scalaでirof式。
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 Main extends App { | |
class irof[A](val condition: Boolean)(block: => A) { | |
lazy val value = block | |
def elof[B, C](elsePart: => B)(implicit e1: A <:< C, e2: B <:< C): C = { | |
if (condition) value else elsePart | |
} | |
} | |
object irof { | |
def apply[A](condition: Boolean)(block: => A): irof[A] = { | |
new irof(condition)(block) | |
} | |
implicit def toAnyVal[A <: AnyVal](irof: irof[A]): AnyVal = { | |
irof.elof(()) | |
} | |
implicit def toAny[A <: Any](irof: irof[A]): Any = { | |
irof.elof(()) | |
} | |
} | |
val x: String = irof(true) { | |
"foo" | |
} elof { | |
"bar" | |
} | |
val y: Any = irof(false) { | |
"foo" | |
} elof { | |
1 | |
} | |
val z: AnyVal = irof(false) { | |
1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment