Created
November 14, 2013 11:02
-
-
Save srenault/7464989 to your computer and use it in GitHub Desktop.
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
sealed trait Literal extends Term { | |
def asOpt[A <: Literal](): Option[A] = { | |
this match { | |
case x: A => Some(x) | |
case _ => None | |
} | |
} | |
} |
trait Literal {
def asOpt[A : LiteralCast](): Option[A] = implicitly[LiteralCast[A]].asOpt(this)
}
case class LiteralString(str: String) extends Literal
trait LiteralCast[A] {
def asOpt(lit: Literal): Option[A]
}
implicit object LiteralStringCast extends LiteralCast[LiteralString] {
def asOpt(lit: Literal): Option[LiteralString] = lit match {
case l:LiteralString => Some(l)
case _ => None
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
abstract type pattern A is unchecked since it is eliminated by erasure
[warn] case x: A => Some(x)