Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created January 4, 2011 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kmizu/764667 to your computer and use it in GitHub Desktop.
Save kmizu/764667 to your computer and use it in GitHub Desktop.
trait Or[+A, +B]
case class LOr[+A, +B](a: A) extends Or[A, B]
case class ROr[+A, +B](b: B) extends Or[A, B]
implicit def lOr[A, B](implicit a: A): Or[A, B] = LOr[A, B](a)
implicit def rOr[A, B](implicit b: B): Or[A, B] = ROr[A, B](b)
def valueIsIntOrString[T](value: T)(implicit param: Or[T => String, T => Int]) = param match {
case LOr(t2String) =>
println("value is String")
println(t2String(value))
case ROr(t2Int) =>
println("value is Int")
println(t2Int(value))
}
valueIsIntOrString(100)
valueIsIntOrString("Foo")
// valueIsIntOrString(1.0) // Compilation Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment