Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2012 04:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4303354 to your computer and use it in GitHub Desktop.
Save anonymous/4303354 to your computer and use it in GitHub Desktop.
Scalaでirof式。
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