Reputation: 41939
Given the following trait:
trait Foo[_ <: Product] {}
How can I pattern match on Foo's generic type?
In other words, is there a way to get at Foo
's _
without using run-time reflection?
Upvotes: 1
Views: 213
Reputation: 35463
This is possible, and I still think it's a duplicate as noted by my comment, but wanted to show how you can do it anyway. Credit to om-nom-nom for the original answer:
trait Foo[_ <: Product]
case class Bar(i:Int)
case class Baz(s:String)
val fooBar = new Foo[Bar]{}
val fooBaz = new Foo[Baz]{}
checkType(fooBar)
checkType(fooBaz)
def checkType[T <: Product : TypeTag](foo:Foo[T]){
foo match{
case f if typeOf[T] <:< typeOf[Bar] => println("its a bar")
case f if typeOf[T] <:< typeOf[Baz] => println("its a baz")
}
}
Upvotes: 1
Reputation: 40510
The whole point of _
as a type parameter is to specify that the type is unknown.
Upvotes: 1