Confustion about implicit evidence <:< and and =>

The A => Moo evidence is, as it’s type suggests, an implicit function, meaning that not only types that are a subtype of Moo are allowed, but also types, for which an implicit conversion to Moo is available. So this is a broader restriction than the other two.

Constraining a type parameter with <: or with an implicit <:< are equivalent semantically. The difference is where they can be used. For a standalone function, there isn’t a real difference that would make <:< useful, but it allows you to restrain type parameters, that arent’t part of the method, but of a parent class.

There are examples for this in the standard library, e.g. in collection classes. You can convert a List[A] to a Map, if A is a tuple type. But you wouldn’t want to restrict A to tuples for all lists. So <:< is used, to apply this restriction on A only for usages of the toMap method, even if A is a parameter of the List.

Also see the answers to this SO question for more examples of <:< use cases.

2 Likes