繁体   English   中英

为什么更喜欢Typeclass而不是继承呢?

[英]Why prefer Typeclass over Inheritance?

根据Erik Osheim的幻灯片 ,他说继承可以解决与类型类相同的问题,但提到继承有一个问题叫做:

脆弱的遗产噩梦

并说继承是

将多态性与成员类型紧密耦合

他的意思是什么?


在我看来,继承很擅长扩展,要么改变现有类型的实现,要么添加新的成员类型(子类型)到接口。

trait Foo { def foo }

class A1 extends Foo{
  override def foo: Unit = ???
}

//change the foo implementation of the existing A1
class A2 extends A1 with Foo{  
  override def foo = ???
}

// add new type B1 to Fooable family
class Bb extends Foo{        
  override def foo = ???
}

现在就类型类而言:

trait Fooable[T] { … }
def foo[T:Fooable](t:T) = …

class Aa {…}
class Bb {…}
object MyFooable {
  implicit object AaIsFooable extends Fooable[Aa]
  implicit object B1IsFooable extends Fooable[Bb]
   …
}

我没有看到任何理由更喜欢Typeclass,我错过了什么?

当使用继承来实现ad-hoc多态时,我们可能需要严重污染我们的值对象的接口。

假设我们想要实现一个实数和一个复数。 没有任何功能,这就像写作一样简单

case class Real(value: Double)

case class Complex(real: Double, imaginary: Double)

现在假设我们要实现添加

  • 两个实数
  • 一个真实和复杂的数字
  • 两个复数

使用继承的解决方案( 编辑:实际上,我不确定这是否可以称为继承,因为方法中add的特征没有实现。但是,在这方面,示例与Erik Orheim的示例没有区别)看起来像这个:

trait AddableWithReal[A] {
  def add(other: Real): A
}

trait AddableWithComplex[A] {
  def add(other: Complex): A
}

case class Real(value: Double) extends AddableWithComplex[Complex] with AddableWithReal[Real] {
  override def add(other: Complex): Complex = Complex(value + other.real, other.imaginary)

  override def add(other: Real): Real = Real(value + other.value)
}

case class Complex(real: Double, imaginary: Double) extends AddableWithComplex[Complex] with AddableWithReal[Complex] {
  override def add(other: Complex): Complex = Complex(real + other.real, imaginary + other.imaginary)

  override def add(other: Real): Complex = Complex(other.value + real, imaginary)
}

因为add的实现与RealComplex紧密耦合,所以每次添加新类型(例如,整数)并且每次需要新操作(例如,减法)时,我们必须扩大它们的接口。

类型类提供了一种将实现与类型分离的方法。 例如,我们可以定义特征

trait CanAdd[A, B, C] {
  def add(a: A, b: B): C
}

并使用implicits单独实现添加

object Implicits {
  def add[A, B, C](a: A, b: B)(implicit ev: CanAdd[A, B, C]): C = ev.add(a, b)
  implicit object CanAddRealReal extends CanAdd[Real, Real, Real] {
    override def add(a: Real, b: Real): Real = Real(a.value + b.value)
  }
  implicit object CanAddComplexComplex extends CanAdd[Complex, Complex, Complex] {
    override def add(a: Complex, b: Complex): Complex = Complex(a.real + b.real, a.imaginary + b.imaginary)
  }
  implicit object CanAddComplexReal extends CanAdd[Complex, Real, Complex] {
    override def add(a: Complex, b: Real): Complex = Complex(a.real + b.value, a.imaginary)
  }
  implicit object CanAddRealComplex extends CanAdd[Real, Complex, Complex] {
    override def add(a: Real, b: Complex): Complex = Complex(a.value + b.real, b.imaginary)
  }
}

这种脱钩至少有两个好处

  1. 防止RealComplex界面的污染
  2. 允许引入新的CanAdd功能,但无法修改可添加的类的源代码

例如,我们可以定义CanAdd[Int, Int, Int]来添加两个Int值而无需修改Int类:

implicit object CanAddIntInt extends CanAdd[Int, Int, Int] {
  override def add(a: Int, b: Int): Int = a + b
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM