通过使用traits和with-keyword减少代码

时间:2010-10-30 11:43:24

标签: scala traits reduce

我有一些具有相同超类型的类。因此,所有这些类都必须覆盖相同的方法。现在我可以调用一个方法并将其提交给普通超类型的对象。但是对每个提交的类型做出反应并不总是有用的,因此会引发异常。首先,我试图像这样解决这个问题:

def operation(s: SuperType) = s match {
  case t: SubType1 => ...
  case t: SubType2 => ...
  case _ => ...
}

由于有很多子类型,这将导致很多代码(在每个方法和每个类中),我试图用traits来解决这个问题。每个特征应该只测试一种类型,然后将对象转发到堆栈上的更高方法。下面的代码描述了我的想象。但这不起作用,因为编译器无法解散类型。另一个问题是我必须在每个行为类中声明类的每个属性。

object TraitWithTest {
  def main(args: Array[String]) {
    val e1 = Even(2, 4)
    val e2 = Even(1, 3)
    val o1 = Odd(1.25, 3.75)
    val o2 = Odd(7.25, 9.25)
    val a1 = All(5.5)
    val a2 = All(3.5)

    println("e1 + e2: " + (e1 + e2))
    println("o1 + o2: " + (o1 + o2))
    try { println("e1 + o2: " + (e1 + o2)) } catch { case e => println(e) }
    println("o1 + e2: " + (o1 + e2))
    println("a1 + e1: " + (a1 + e2))
  }
}

abstract class Num {
  def +(n: Num): Num
}

trait OddBehaviour extends Num {
  val e1, e2: Int // here I don't want to declare all attributes
  val a1: Double
  abstract override def +(n: Num) = n match {
    case o: Odd => throw new UnsupportedOperationException("Even#+(Odd)")
    case _ => super.+(n)
  }
}

trait EvenBehaviour extends Num {
  val o1, o2: Double
  val a1: Double
  abstract override def +(n: Num) = n match {
    case e: Even => Odd(o1 + e.e1, o2 + e.e2)
    case _ => super.+(n)
  }
}

trait AllBehaviour extends Num {
  val o1, o2: Double
  val e1, e2: Int
  abstract override def +(n: Num) = n match {
    case a: All => Odd(o1 + a.a1, o2 + a.a1)
    case _ => super.+(n)
  }
}

object Even {
  def apply(e1: Int, e2: Int) = new Even(e1, e2) with OddBehaviour with AllBehaviour
}

abstract case class Even(e1: Int, e2: Int) extends Num {
  override def +(n: Num) = n match {
    case c: Even => Even(e1 + c.e1, e2 + c.e2)
    case _ => throw new IllegalArgumentException
  }
}

object Odd {
  def apply(o1: Double, o2: Double) = new Odd(o1, o2) with EvenBehaviour with AllBehaviour
}

abstract case class Odd(o1: Double, o2: Double) extends Num {
  override def +(n: Num) = n match {
    case o: Odd => Odd(o1 + o.o1, o2 + o.o2)
    case _ => throw new IllegalArgumentException
  }
}

object All {
  def apply(a1: Double) = new All(a1) with EvenBehaviour with OddBehaviour
}

abstract case class All(a1: Double) extends Num {
  override def +(n: Num) = n match {
    case a: All => All(a1 + a.a1)
    case _ => throw new IllegalArgumentException
  }
}

有人可以告诉我是否可以通过使用特征减少代码行数?或者是我目前使用最好的全匹配解决方案?

修改

在你的帮助下,我找到了一个半工作的解决方案。我的主要问题是我试图通过使用Scala功能来减少代码行。所以我忽略了最简单的方法:外包代码!我只需要创建一个检查对象组合的新对象。对象本身只处理自己的类型。

这是代码:

final object TraitWithTest {
  def main(args: Array[String]) {
    import traitwith.operations._
    val e1 = Even(2, 4)
    val e2 = Even(1, 3)
    val o1 = Odd(1.25, 3.75)
    val o2 = Odd(7.25, 9.25)
    val a1 = All(5.5)
    val a2 = All(3.5)

    val n1 = NumHolder(o1)
    val n2 = NumHolder(a1)

    println("e1 + e2: " + add(e1, e2))
    println("o1 + o2: " + add(o1, o2))
    try { println("e1 + o2: " + add(e1, o2)) } catch { case e => println(e) }
    println("o1 + e2: " + add(o1, e2))
    try { println("a1 + e2: " + add(a1, e2)) } catch { case e => println(e) }
    println("n1 + n2: " + add(n1, n2))
  }
}

final object operations {
  def add(a: Num, b: Num) = a -> b match {
    case (a1: Odd, b1: Odd) => a1 + b1
    case (a1: Odd, b1: Even) => Odd(a1.x + b1.x, a1.y + b1.y)
    case (a1: Odd, b1: All) => Odd(a1.x + b1.x, a1.y + b1.x)
    case (a1: Even, b1: Even) => a1 + b1
    case (a1: All, b1: All) => a1 + b1
    case _ => error("can't add " + b + " to " + a)
  }
}

abstract class Num {
  type A <: Num
  def +(a: A): A
}

final case class Odd(x: Double, y: Double) extends Num {
  override type A = Odd
  override def +(a: Odd) = Odd(x + a.x, y + a.y)
}

final case class Even(x: Int, y: Int) extends Num {
  override type A = Even
  override def +(a: Even) = Even(x + a.x, y + a.y)
}

final case class All(x: Double) extends Num {
  override type A = All
  override def +(a: All) = All(x + a.x)
}

final case class NumHolder(x: Num) extends Num {
  override type A = NumHolder
  override def +(a: NumHolder) = NumHolder(x + a.x)
}

我稍微扩展了代码并插入了对象NumHolder。现在,只有一个小缺陷:在NumHolder中,如果没有在add-method中收到编译错误,我就无法提交超类型。我尝试使用Generics而不是type-keyword,但这是不方便的,因为我总是将类型设置为Num(也在对象 operations 中)。

如何解决这个小编译错误?

6 个答案:

答案 0 :(得分:5)

您的问题是您正在尝试使用面向对象的功能,例如类和继承,其设计不是面向对象的。

OOP的整个是你没有内省类的内容。相反,使用多态来实现结果。我特别喜欢this paper来说明OO应该如何运作,但在这方面并不缺乏资源。

修改

例如,提供的代码大致转换为以下内容,减去不起作用的内容(提供的代码因为它们而无法精确编译)。

abstract class Num {
  def +(n: Num): Num
  def plus(n1: Int, n2: Int): Num
  def plus(n1: Double, n2: Double): Num
  def plus(n: Double): Num
}

case class Even(e1: Int, e2: Int) extends Num {
  override def +(n: Num) = n.plus(e1, e2)
  override def plus(n1: Int, n2: Int) = Even(e1 + n1, e2 + n2)
  override def plus(n1: Double, n2: Double) = Odd(n1 + e1, n2 + e2)
  // the code provided references o1 and o2, which are not defined anywhere for Even
  // so I'm providing an alternate version
  override def plus(n: Double) = Odd(n + e1, n + e2)
}

case class Odd(o1: Double, o2: Double) extends Num {
  override def +(n: Num) = n.plus(o1, o2)
  override def plus(n1: Int, n2: Int) = throw new UnsupportedOperationException("Even#+(Odd)")
  override def plus(n1: Double, n2: Double) = Odd(o1 + n1, o2 + n2)
  override def plus(n: Double) = throw new UnsupportedOperationException("Even#+(Odd)")
}

case class All(a1: Double) extends Num {
  override def +(n: Num) = n.plus(a1)
  // the code provided references o1 and o2, which are not defined anywhere for All
  // so I'm providing an alternate version
  override def plus(n1: Int, n2: Int) = Odd(a1 + n1, a1 + n2)
  override def plus(n1: Double, n2: Double) = Odd(n1 + a1, n2 + a1)
  override def plus(n: Double) = All(a1 + n)
}

在我看来它可以通过访问者模式进一步改进,这是有意义的,因为它针对类型匹配通常所做的相同问题。

答案 1 :(得分:3)

错误引用某个广告:有一个类型类...

Scala已经通过Numeric支持“数字”上的ad-hoc多态,这可能是你真正想要的:http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/scala/math/Numeric.html

但是如果这个Even / Odd / All方案就是你实际做的,而不仅仅是一个人为的例子,那么你总是可以推出自己的类型类!


我们称之为Addable

case class Even(x:Int, y:Int)
case class Odd(x:Double, y:Double)
case class All(x:Double)

abstract class Addable[A, B] {
  def add(a: A, b: B): A
}

implicit object EvenCanAddEven extends Addable[Even, Even] {
  def add(a:Even, b:Even) = Even(a.x+b.x, a.y+b.y)
}

implicit object OddCanAddOdd extends Addable[Odd, Odd] {
  def add(a:Odd, b:Odd) = Odd(a.x+b.x, a.y+b.y)
}

implicit object OddCanAddEven extends Addable[Odd, Even] {
  def add(a:Odd, b:Even) = Odd(a.x+b.x, a.y+b.y)
}

implicit object AllCanAddAll extends Addable[All, All] {
  def add(a:All, b:All) = All(a.x+b.x)
}

def add[A,B](a:A, b:B)(implicit tc: Addable[A,B]) =
  tc.add(a, b)


val e1 = Even(2, 4)
val e2 = Even(1, 3)
val o1 = Odd(1.25, 3.75)
val o2 = Odd(7.25, 9.25)
val a1 = All(5.5)
val a2 = All(3.5)

println("e1 + e2: " + add(e1, e2))
println("o1 + o2: " + add(o1, o2))
println("e1 + o2: " + add(e1, o2)) //compiler should fail this line
println("o1 + e2: " + add(o1, e2))
println("a1 + e1: " + add(a1, e2))

免责声明: 我实际上没有测试过代码,这台机器还没有安装Scala

答案 2 :(得分:3)

替代解决方案,用于何时直到运行时才知道类型:

sealed trait Num
case class Even(x:Int, y:Int) extends Num
case class Odd(x:Double, y:Double) extends Num
case class All(x:Double) extends Num

object operations {
  def add(a: Num, b: Num) : Num = (a,b) match {
    case (a1:Even, b1:Even) => Even(a1.x+b1.x, a1.y+b1.y)
    case (a1:Odd, b1:Odd) => Odd(a1.x+b1.x, a1.y+b1.y)
    case (a1:Odd, b1:Even) => Odd(a1.x+b1.x, a1.y+b1.y)
    case (a1:All, b1:All) => All(a1.x, b1.x)
    case _ => error("can't add " + a + " to " + b)
  }
}

这里的诀窍是首先将两个参数包装到一个元组中,然后让一个对象进行模式匹配。

<强>更新

按照您的编辑;您似乎不需要抽象类型A,为什么不将Num作为标记特征并在每个子类中单独定义+方法?

sealed abstract trait Num

case class Odd(x: Double, y: Double) extends Num {
  def +(a: Odd) = Odd(x + a.x, y + a.y)
}

final case class Even(x: Int, y: Int) extends Num {
  def +(a: Even) = Even(x + a.x, y + a.y)
}

final case class All(x: Double) extends Num {
  def +(a: All) = All(x + a.x)
}

final case class NumHolder(x: Num) extends Num {
  def +(a: NumHolder) = NumHolder(x + a.x)
}

答案 3 :(得分:2)

我不知道我是否可以解决你的问题,但在考虑这个问题时,我试图至少让你的例子进行编译和工作,希望至少在某种程度上有所帮助。

我以toString方法的形式添加了一些噪音,以便能够查看实例化和表达式的结果。

abstract class Num(val a: Double, val b: Double) {
  def +(that: Num): Num
  override def toString = (<z>Num({a}, {b})</z> text)
}

“最后的手段”类All应该是一个案例类,以使匹配更平滑,但因为它将继承为真正的案例类将无法正常工作。使用applyunapply方法的伴随对象可以解决这个问题。

All可以处理类似字词,但不会尝试处理EvenOdd字词,因为这些字词同时是All个。{ / p>

class All(override val a: Double, override val b: Double) extends Num(a, b) {
  def +(that: Num): Num = that match {
    case All(n) => All(this.a + n)
    case _      => error("I don't know this subtype")
  }
  override def toString = (<z>All({a})</z> text)
}
object All {
  def apply(num: Double) = new All(num, num)
  def unapply(num: All) = Some(num.a)
}

现在,EvenOdd的工作方式可以被提炼成特征,但这个例子没有必要。不这样做简化了继承,但可能违背了示例的要点,我不知道。

Even知道如何处理EvenOdd条款,但将其他条款传递给其超类。同样,它是一个用于匹配目的的虚假案例类。

class Even(override val a: Double, override val b: Double) extends All(a, b) {
  override def +(that: Num): Num = that match {
    case Even(a, b) => Even(this.a + a, this.b + b)
    case Odd(a, b)  => Odd(this.a + a, this.b + b)
    case x => super.+(x)
  }
  override def toString = (<z>Even({a}, {b})</z> text)
}
object Even {
  def apply(a: Double, b: Double) = new Even(a, b)
  def unapply(num: Even) = Some((num.a, num.b))
}

Odd知道如何处理Even条款,但拒绝处理Odd条款(我从你的例子中改变了这一点,以便制作弱双关语,欢迎你)。

class Odd(override val a: Double, override val b: Double) extends All(a, b) {
  override def +(that: Num): Num = that match {
    case Even(a, b) => Odd(this.a + a, this.b + b)
    case Odd(a, b)  => error("Adding two Odds is an odd thing to do")
    case x => super.+(x)
  }
  override def toString = (<z>Odd({a}, {b})</z> text)
}
object Odd {
  def apply(a: Double, b: Double) = new Odd(a, b)
  def unapply(num: Odd) = Some((num.a, num.b))
}

好的,让我们一起旋转。

object Try {
  def main(args: Array[String]) {
    val e1 = Even(2, 4)
    val e2 = Even(1, 3)
    val o1 = Odd(1.25, 3.75)
    val o2 = Odd(7.25, 9.25)
    val a1 = All(5.5)
    val a2 = All(3.5)

    println("e1 + e2: " + (e1 + e2))
    println("e1 + o2: " + (e1 + o2))
    try { println("o1 + o2: " + (o1 + o2)) } catch { case e => println(e) }
    println("o1 + e2: " + (o1 + e2))
    println("a1 + e1: " + (a1 + e2))
  }
}

答案 4 :(得分:0)

看起来仿制药可能对您有所帮助。尝试这样的事情:

class Supertype[A <: Supertype] {
    def operation(s: A) {

    }
}

class Subtype extends SuperType[Subtype] {
    override def operation(s: Subtype) {

    }
}

你的问题描述不是很清楚,所以这是一个猜测......

答案 5 :(得分:0)

基于Kevin Wright的answer我现在解决了这个问题:

package de.traitwith

import de.traitwith.Operations._
import de.traitwith._

object TraitWithTest {
  def main(args: Array[String]) {
    val e1 = Even(2, 4)
    val e2 = Even(1, 3)
    val o1 = Odd(1.25, 3.75)
    val o2 = Odd(7.25, 9.25)
    val a1 = All(5.5)
    val a2 = All(3.5)
    val n1 = NumHolder(o1)
    val n2 = NumHolder(a1)
    println("e1 + e2: " + add(e1, e2))
    println("o1 + o2: " + add(o1, o2))
    try { println("e1 + o2: " + add(e1, o2)) } catch { case e => println(e) }
    println("o1 + e2: " + add(o1, e2))
    try { println("a1 + e2: " + add(a1, e2)) } catch { case e => println(e) }
    println("n1 + n2: " + add(n1, n2))
    println("o1 + n2: " + add(o1, n2))
  }
}

object Operations {
  def add(a: Num, b: Num): Num = a -> b match {
    case (a1: Odd, b1: Odd) => a1 + b1
    case (a1: Odd, b1: Even) => Odd(a1.x + b1.x, a1.y + b1.y)
    case (a1: Odd, b1: All) => Odd(a1.x + b1.x, a1.y + b1.x)
    case (a1: Odd, b1: NumHolder) => add(a1, b1.x)
    case (a1: Even, b1: Even) => a1 + b1
    case (a1: Even, b1: NumHolder) => add(a1, b1.x)
    case (a1: All, b1: All) => a1 + b1
    case (a1: All, b1: NumHolder) => add(a1, b1.x)
    case (a1: NumHolder, b1: NumHolder) => a1 + b1
    case (a1: NumHolder, b1: Odd)=> add(a1.x, b1)
    case (a1: NumHolder, b1: Even) => add(a1.x, b1)
    case (a1: NumHolder, b1: All) => add(a1.x, b1)
    case _ => error("can't add " + b + " to " + a)
  }
}

abstract class Num

final case class Odd(x: Double, y: Double) extends Num {
  def +(a: Odd) = Odd(x + a.x, y + a.y)
}

final case class Even(x: Int, y: Int) extends Num {
  def +(a: Even) = Even(x + a.x, y + a.y)
}

final case class All(x: Double) extends Num {
  def +(a: All) = All(x + a.x)
}

final case class NumHolder(x: Num) extends Num {
  def +(a: NumHolder) = NumHolder(add(x, a.x))
}

我在超类型中没有更多的方法 - 我希望,同一天,这不会造成问题。可以删除类中的所有add方法,但在我的实际应用程序中,我有更大的类,所以我需要它们。