Skip to content

Instantly share code, notes, and snippets.

Created April 20, 2013 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5427293 to your computer and use it in GitHub Desktop.
Save anonymous/5427293 to your computer and use it in GitHub Desktop.
It prints out three WTF's, I expected STRING! STRING! LONG! How to deal with type erasure in this case?
case class Column[T](s: Option[T] = None)
class Def[C](implicit desired: Manifest[C]) {
def unapply[X](c: X)(implicit m: Manifest[X]): Option[C] = {
def sameArgs = desired.typeArguments.zip(m.typeArguments).forall { case (desired, actual) => desired >:> actual }
if (desired >:> m && sameArgs) Some(c.asInstanceOf[C])
else None
}
}
object TestApp {
val LongTest = new Def[Column[Long]]
val StringTest = new Def[Column[String]]
val usersTable = List(
'email -> Column[String](),
'name -> Column[String](),
'id -> Column[Long]())
def main(args: Array[String]) {
usersTable.foreach(coldef => {
val (s: Symbol, f: Column[_]) = coldef
f match {
case LongTest(f) => println("LONG!")
case StringTest(f) => println("STRING!")
case _ => println("WTF")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment