These classes are used for implicit parameters that limit the applicability of the method. The following is a description of each class. In general, they are useful for restricting a type parameter of an enclosing class in the context of a single method.
<:<[A,B] or A <:< B
The compiler can provide an implicit instance of this type only when A is a subtype of B. This is similar to A <: B in the type parameter list.
This can be useful if you want to add an additional constraint for the class type parameter in the context of a particular method. For example, the Foo class below can be used with any type, but the bar method is valid only when T is a subtype of Number .
class Foo[T](x: T) { // In general T could be any type def bar(implicit ev: T <:< Number) = { // This method can now only be used when T is a subtype of Number // also, you can use ev to convert a T to number ev(x).doubleValue } } new Foo(123 : java.lang.Integer).bar // returns 123.0: Double new Foo("123").bar // compile error: Cannot prove java.lang.String <:< java.lang.Number
=:=[A,B] or A =:= B
The compiler can provide an implicit instance of this type only when A is the same type as B. It does not have equivalent syntax in the list of type parameters, you just have to use the same type parameter twice.
This can be used in the same way as <:< , except that it requires the types to match exactly. This can be used to make a couple of methods mutually exclusive.
class Foo[T<:Number](x:T) { def numOnly(implicit ev: T =:= Number) = () def floatOnly(implicit ev: T =:= Float) = () } val asFloat = new Foo(123.0f:java.lang.Float) asFloat.numOnly // Compile error asFloat.floatOnly // Ok val asNum = new Foo(123.0f:java.lang.Number) asFloat.floatOnly // Ok asFloat.numOnly // Compile error
Essentially, if a type parameter is more specific than a constraint, you can force a more specific method.
<%<[A,B] or A <%< B
The compiler can provide an implicit instance of this type only when A can be converted to B. This is similar to A <% B in the type parameter list.
This requires an implicit function to turn A into B. This will always be possible if A <: B , since the implicit A <:< B satisfies this restriction.
This class is actually marked as deprecated. It says that you should just use A => B