How can I combine the typeclass pattern with subtyping?

后端 未结 1 1199
刺人心
刺人心 2021-02-01 07:52

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         


        
1条回答
  •  -上瘾入骨i
    2021-02-01 08:17

    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

    0 讨论(0)
提交回复
热议问题