繁体   English   中英

在Scala中,如何使用隐式转换将方法“添加”到公共父类的子类?

[英]In Scala, how can I use implicit conversion to “add” methods to subclasses of a common parent?

假设我在“哑”模型中有一些数据。 在这个例子中,我将使用CircleTriangle ,它们扩展了一个trait Shape

我正在寻找一种方法来隔离可能使用这些形状的行为,但我不确定构建它的最佳方法。 如果我试图将这些形状绘制到文档上,我希望能够编写如下代码:

shapes.foreach(doc.add)

这里的诀窍是shapesSeq[Shape] ,而add方法是我想要隐式添加的东西,因为我无法修改形状本身(我也不想将这些特定的功能融入其中)。

我遇到困难的地方是,我不知道如何将隐式转换与子类混合在一起。 有关详细信息,请参阅代码中的QUESTION:

// Let's assume I'm working with some shape models that are defined in some
// external library that's out of my control.
sealed trait Shape
case class Circle() extends Shape
case class Triangle() extends Shape

// Now I'm building an add that adds stuff to a Document
// and I want to locally implement methods that work on these general shapes.
case class Document()

// Using implicit conversion to add methods to a case class that's just holding data
implicit class DocumentExtensions(doc: Document) {
  // I don't want this to be called
  def add(shape: Shape): Unit = println("Add a shape")

  // I want to use shape-specific methods
  def add(shape: Circle): Unit = println("Add a circle")
  def add(shape: Triangle): Unit = println("Add a triangle")
}

val doc = Document()
val shapes = Seq(Circle(), Triangle())

// This just prints "Add a shape" for the Circle and Triangle.
// I want to it to print "Add a circle" and "Add a triangle".
shapes.foreach { shape =>
  // QUESTION:
  // Is there a way or pattern to have this call the add for the
  // subclass instead of for Shape? I want this to be fully dynamic
  // so that I don't have to list out each subclass. Even more ideally,
  // the compiler could warn me if there was a subclass that there wasn't
  // an implicit add for.
  doc.add(shape)
}

// This would work, but I'm wondering if there's a way to do this more
// dynamically without listing everything out.
shapes.foreach {
  case c: Circle => doc.add(c)
  case t: Triangle => doc.add(t)
}

我确定我正在寻找什么名称,但我不知道它是什么或搜索什么。

问题:编译器无法选择和使用特定于处理子类的隐式值。 当你只知道它是一个Shape时,基本上不可能决定调用什么方法(对于TriangleCircle )。 这实际上是一个经典问题,它有标准的解决方案。

解决方案1

DocumentExtension.add模式匹配

优点:

  1. 由于您的trait Shape被定义为sealed ,如果您错过了某个祖先的案例,编译器会给您。
  2. 分类定义和动作处理

缺点:

  1. 要求列出特征的所有子类的Boilerplate

解决方案2

古典访客模式

sealed trait Shape {
  def addToDoc(doc: Document, visitor: ShapeDrawer)
}
final class Triangle extends Shape {
  def addToDoc(doc: Document, visitor: ShapeDrawer) = visitor.draw(doc, this)
}
final class Circle extends Shape {
  def addToDoc(doc: Document, visitor: ShapeDrawer) = visitor.draw(doc, this)
}

trait ShapeDrawer {
  def draw(doc: Document, t: Circle)
  def draw(doc: Document, t: Triangle)
}

val drawer: ShapeDrawer = ???
val doc: Document = ???
val shapes = Seq.empty[Shape]

shapes.foreach(_.addToDoc(doc, drawer))

此解决方案还符合在编译时确保已处理Shape的每个子类的要求,但需要向特征本身添加奇怪的方法。

暂无
暂无

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

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