简体   繁体   English

在Scala中使用代数数据类型时在特征中定义通用方法

[英]Defining a Generic Method in a Trait When Using Algebraic Data Type in Scala

I have defined an abstract data type, and i wanted to make it fairly general and scalable. 我已经定义了一个抽象数据类型,我想让它相当通用和可扩展。

However, scala type system is giving me a nightmare. 然而,scala类型系统给了我一个噩梦。

sealed trait Tree[+A] {
  def evaluateTree(): A = this match {
    case Leaf(value) => value
    case Branch_A1(op, left) => op(evaluateTree(left))
    case Branch_A2(op, left, right) => op(evaluateTree(left),evaluateTree(right))
  }
}
case object EmptyTree extends Tree[Nothing]
case class Leaf[A](value: A) extends Tree[A]
case class Branch_A1[A](op: A => A, left: Tree[A]) extends Tree[A]
case class Branch_A2[A](op: (A,A) => A, left: Tree[A], right: Tree[A]) extends Tree[A]

A would be eg Double. A将是例如Double。 So in this case i have a tree which has branches that represent functions of one and two arguments (Branch_A1, Branch_A2) 所以在这种情况下我有一个树,它有一个分支代表一个和两个参数的函数(Branch_A1,Branch_A2)

However it does not compile. 但是它没有编译。 In op(evaluateTree(left)) it says that "it cannot resolve ... with such reference". op(evaluateTree(left))它说“它无法解决......带有这样的引用”。

I could take the function away from the class into a more functional pattern but i want to do it following an object design. 我可以把这个函数从类中转移到一个更具功能性的模式,但我想在对象设计之后做到这一点。 The only way i can get it into compiling is to do: 我可以进入编译的唯一方法是:

sealed trait Tree[+A] {
  def evaluateTree(t: Tree[A]): A = this match {
    case Leaf(value) => value
    case Branch_A1(op, left) => op(evaluateTree(left))
    case Branch_A2(op, left, right) => op(evaluateTree(left),evaluateTree(right))
  }
}
case object EmptyTree extends Tree[Nothing]
case class Leaf[A](value: A) extends Tree[A]
case class Branch_A1[A](op: A => A, left: Tree[A]) extends Tree[A]
case class Branch_A2[A](op: (A,A) => A, left: Tree[A], right: Tree[A]) extends Tree[A]

which is stupid since i need to pass it the instance of the object. 这是愚蠢的,因为我需要传递它的对象的实例。

I need to tell the compiler that this is an Tree[A] . 我需要告诉编译器this是一个Tree[A] So i have tried: 所以我尝试过:

sealed trait Tree[+A] {

  this: Tree[A] =>

  def evaluateTree(): A = this match {
    case Leaf(value) => value
    case Branch_A1(op, left) => op(evaluateTree(left))
    case Branch_A2(op, left, right) => op(evaluateTree(left),evaluateTree(right))
  }
}

Still the compiler does not let me go, the error is the same actually. 仍然编译器不让我走,错误实际上是相同的。 How can i solve this?? 我怎么解决这个?

It looks like your receivers are screwed up. 看起来你的接收器搞砸了。 evaluateTree does not take arguments. evaluateTree不接受参数。 I'm assuming you want to evaluate the subtree, as in op(evaluateTree(left)) should instead be op(left.evaluateTree()) 我假设你想要评估子树,因为在op(evaluateTree(left))应该是op(left.evaluateTree())

def evaluateTree(): A = this match {
  case Leaf(value) => value
  case Branch_A1(op, left) => op(left.evaluateTree())
  case Branch_A2(op, left, right) => op(left.evaluateTree(),right.evaluateTree())
}

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

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