1) check if B is the same type as A
class Foo[A] { def foo(param: SomeClass[A]) = ??? } // or class Foo[A] { def foo[B](param: SomeClass[B])(implicit ev: A =:= B) = ??? }
2) check if B is a subtype of A?
class Foo[A] { def foo[B <: A](param: SomeClass[B]) = ??? } // or class Foo[A] { def foo[B](param: SomeClass[B])(implicit ev: B <:< A) = ??? }
In your case, you do not need restrictions of a generalized type (i.e. =:=
, <:<
). They are needed when you need to add a constraint on a type parameter that is defined elsewhere, and not on a method.
eg. To ensure A
is String
:
class Foo[A] { def regularMethod = ??? def stringSpecificMethod(implicit ev: A =:= String) = ??? }
Here you cannot enforce a type constraint without a generic type constraint.
source share