Pattern matching on generic type in Scala

I would go with TypeTag if you’re on 2.10+ import reflect.runtime.universe._ class Observable[Foo] def X[T: TypeTag](ob: Observable[T]) = ob match { case x if typeOf[T] <:< typeOf[Double] => println(“Double obs”) case x if typeOf[T] <:< typeOf[Boolean] => println(“Boolean obs”) case x if typeOf[T] <:< typeOf[Int] => println(“Int obs”) } X(new Observable[Int]) // Int obs See … Read more

How does Gson TypeToken work?

From §4.6 of the JLS (emphasis mine): Type erasure is a mapping from types (possibly including parameterized types and type variables) to types (that are never parameterized types or type variables). We write |T| for the erasure of type T. The erasure mapping is defined as follows: The erasure of a parameterized type (§4.5) G … Read more