Suppose I\'m using the typeclass pattern in Scala. Here\'s how I make a class C part of the typeclass Foo:
Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64
There are different possible solutions for this, depending on whether I want to fix the problem only for C, or whether I want to fix the problem for the entire typeclass.
For C only, instead of implicit object FooC ...
we say:
implicit def CIsFoo[T <: C]: Foo[T] =
new Foo[T] { override def foo(t: T) { println("it's a C!") } }
To fix all of Foo, make it contravariant:
trait Foo[-T] { def foo(t: T) }
Or if for some reason you can't or don't want to do that, you can replace def foo...
with:
def foo[T](t: T)(implicit foo: Foo[_ >: T]) =
foo.foo(t)
(Thanks to #scala denizens Daniel Sobral and Stefan Zeiger for their help.)
UPDATED Sep 20 2011 to include the "make Foo contravariant" solution, which I missed