模块化Scala设计:如何避免构造函数“推出”样板文件?

12

也许一位对Scala具有优雅风格感觉的专家可以帮我找出更好的方法来构造下面这个代码,它存在一个构造函数“推出”的问题。

我们从一个简单的基类开始:

class Foo(val i: Int, val d: Double, val s: String) {

  def add(f: Foo) = new Foo(i + f.i, d + f.d, s + f.s)
  override def toString = "Foo(%d,%f,%s)".format(i,d,s)

}

为了在一个复杂的应用程序中进行类型检查,我需要一个没有任何额外状态的子类:

class Bar(i: Int, d: Double, s: String) extends Foo(i,d,s) {

  override def toString = "Bar(%d,%f,%s)".format(i,d,s)

}

目前情况是,当我添加两个Bars时,我只会得到一个Foo:

val x = new Bar(1,2.3,"x")
val y = new Bar(4,5.6,"y")
val xy = x.add(y)

在 REPL 中得到以下响应:

x  : Bar = Bar(1,2.300000,x)
y  : Bar = Bar(4,5.600000,y)
xy : Foo = Foo(5,7.900000,xy)

我该如何优雅地使两个Bar相加组成一个新的Bar(而不是Foo),而不必复制并粘贴Foo的add方法,以下是代码示例?

class Bar(i: Int, d: Double, s: String) extends Foo(i,d,s) {

  // ugly copy-and-paste from Foo:
  def add(b: Bar) = new Bar(i + b.i, d + b.d, s + b.s)
  override def toString = "Bar(%d,%f,%s)".format(i,d,s)

}

我有许多这样的Bar(本质上都是Foo的副本,但对于类型检查非常重要),一个无需复制和粘贴的解决方案将会收到丰厚的回报。

谢谢!

3个回答

12
我尽可能避免继承。因此,这里提供了另一种方法。
BarFoo具有完全相同的构造函数,并且两者都是无状态的。如果您想要多个子类型来传递任何其他信息,可以使用通用参数作为“标签”。例如:
trait Kind
trait Bar extends Kind

class Foo[T<:Kind](val i: Int, val d: Double, val s: String) {
   def add(f: Foo[T]) = new Foo[T](i + f.i, d + f.d, s + f.s)
   override def toString = "Foo(%d,%f,%s)".format(i,d,s)
}


scala> val b1 = new Foo[Bar](2,3.0,"hello")
b1: Foo[Bar] = Foo(2,3.000000,hello)

scala> val b2 = new Foo[Bar](3,1.0," world")
b2: Foo[Bar] = Foo(3,1.000000, world)

scala> b1 add b2
res1: Foo[Bar] = Foo(5,4.000000,hello world)

现在add是类型安全的。然后您可以使用类型类来获取toString以显示Kind

我喜欢“参数作为标签”的想法,因为它可以提高类型安全性。谢谢! - Perfect Tiling

5
扩展@paradigmatic的答案,如果您想支持针对每个Bar特定的操作(例如不同的toString),则可以更进一步,将Kind变成一个类型类。
trait Kind[T] { def name : String }
trait Bar
implicit object BarHasKind extends Kind[Bar] { val name = "Bar" }

class Foo[T : Kind](val i : Int, val d : Double, val s : String) {
  def add(f : Foo[T]) = new Foo[T](i + f.i, d + f.d, s + f.s)
  override def toString = implicitly[Kind[T]].name + "(%d,%f,%s)".format(i,d,s)
}

scala> val b1 = new Foo[Bar](2, 3.0, "hello")
b1: Foo[Bar] = Bar(2,3.000000,hello)

trait Biz
implicit object BizHasKind extends Kind[Biz] { val name = "Biz" }

scala> val b2 = new Foo[Biz](1, 1.0, "One")

它和以前一样类型安全:

scala> b1 add b2
<console>:16: error: type mismatch;
  found   : Foo[Biz]
  required: Foo[Bar]

scala> b2 add b2
resN: Foo[Biz] = Biz(2,2.000000,OneOne)

如果您希望某个属性与标记相关联,就在Kind中抽象声明它们,并在隐式对象中提供实现。


有趣。您的“隐式”方法相较于在Foo内声明'def name ="Foo"'方法并让Bar和Biz进行覆盖,有什么优势吗? - Perfect Tiling
1
这并不是真正的优势,我只是想表明即使在没有继承的情况下,也可以实现完全相同的效果,类似于被接受答案中提出的那种设置。 - Philippe

2
使用类型参数化的方法有一个限制,即不能以继承的方式自然地扩展功能。因此,另一种替代方法是可重写的工厂方法(它只是构造函数的委托):
class Foo(val i: Int, val d: Double, val s: String) {

  protected def create(i: Int, d: Double, s: String) = new Foo(i, d, s)

  def add[A <: Foo](f: A) = create(i + f.i, d + f.d, s + f.s)

  override def toString = "Foo(%d,%f,%s)".format(i,d,s)
}

class Bar(i: Int, d: Double, s: String) extends Foo(i,d,s) {

  protected override def create(i: Int, d: Double, s: String) = new Bar(i, d, s)

  override def toString = "Bar(%d,%f,%s)".format(i,d,s)

  // additional methods...

}

println( new Foo(10, 10.0, "10") add new Bar(10, 10.0, "10") )
println( new Bar(10, 10.0, "10") add new Foo(10, 10.0, "10") )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接