I saw something interesting this morning when I tried to create a container for a user-defined class that mixed in Scala's Numeric trait. Consider a class called MyNum (see MyNum.scala) that mixes-in Numeric. I wanted to create a container that could hold any type that mixed in the Numeric trait, including MyNum. My first intuition was to use a context bound on the type parameter. However, this seemed to only work for built-in Numeric types.
In order to fix the problem, I tried changing the context bound to a view bound. Viola, it worked!
In the following code, the commented out line will not compile. MyNumContainerB is able to hold both built-in and user-derived Numeric types.
error: ambiguous implicit values: both method stringCanBuildFrom in object Predef of type => scala.collection.generic.CanBuildFrom[String,Char,String] and method conforms in object Predef of type [A]=> <:<[A,A] match expected type
val d = new MyNumContainerA(c)
I'm still trying to figure out why a context bound doesn't work. Perhaps there is a lack of implicit evidence somewhere? Anyway, some more investigation is needed. If anyone knows why this happening then please let me know!