How can I combine the typeclass pattern with subtyping?

穿精又带淫゛_ 提交于 2019-12-20 09:57:37

问题


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-Bit Server VM, Java 1.6.0_26).

scala> trait Foo[T] { def foo(t: T) }
defined trait Foo

scala> def foo[T : Foo](t: T) { implicitly[Foo[T]].foo(t) }
foo: [T](t: T)(implicit evidence$1: Foo[T])Unit

scala> class C
defined class C

scala> foo(new C)
<console>:11: error: could not find implicit value for evidence parameter of type Foo[C]
       foo(new C)
          ^

scala> implicit object FooC extends Foo[C] { override def foo(c: C) { println("it's a C!") } }
defined module FooC

scala> foo(new C)
it's a C!

So far so good. But suppose I have a subclass D of C, and I want instances of D to be "in" the typeclass too:

scala> class D extends C
defined class D

scala> foo(new D)
<console>:13: error: could not find implicit value for evidence parameter of type Foo[D]
       foo(new D)
          ^

Doh! How do I make this work without having to explicitly provide a typeclass instance for D?


回答1:


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



来源:https://stackoverflow.com/questions/6682824/how-can-i-combine-the-typeclass-pattern-with-subtyping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!