Wei*_*Lin 13 inheritance functional-programming scala typeclass
根据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 = ???
}
Run Code Online (Sandbox Code Playgroud)
现在就类型类而言:
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]
…
}
Run Code Online (Sandbox Code Playgroud)
我没有看到任何理由更喜欢Typeclass,我错过了什么?
Kul*_*mpa 16
当使用继承来实现ad-hoc多态时,我们可能需要严重污染我们的值对象的接口.
假设我们想要实现一个实数和一个复数.没有任何功能,这就像写作一样简单
case class Real(value: Double)
case class Complex(real: Double, imaginary: Double)
Run Code Online (Sandbox Code Playgroud)
现在假设我们要实现添加
使用继承的解决方案(编辑:实际上,我不确定这是否可以称为继承,因为特征中的方法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)
}
Run Code Online (Sandbox Code Playgroud)
因为add的实现紧密耦合,Real并且Complex每次添加新类型时(例如,整数)并且每次需要新操作(例如,减法)时我们必须扩大它们的接口.
类型类提供了一种将实现与类型分离的方法.例如,我们可以定义特征
trait CanAdd[A, B, C] {
def add(a: A, b: B): C
}
Run Code Online (Sandbox Code Playgroud)
并使用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)
}
}
Run Code Online (Sandbox Code Playgroud)
这种脱钩至少有两个好处
Real和界面的污染ComplexCanAdd功能而无法修改可添加的类的源代码例如,我们可以定义CanAdd[Int, Int, Int]添加两个Int值而不修改Int类:
implicit object CanAddIntInt extends CanAdd[Int, Int, Int] {
override def add(a: Int, b: Int): Int = a + b
}
Run Code Online (Sandbox Code Playgroud)