Created
April 20, 2013 20:31
-
-
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?
This file contains 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
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