Case classes matching in Scala

喜欢而已 提交于 2019-12-06 14:57:15

You could simply say:

def sortCodeTreeFun(x: CodeTree, y: CodeTree) = {
  (x, y) match {
    case (_: Leaf, _: Leaf)           => true
    case (x1: CodeTree, y1: CodeTree) => x1.weight < y1.weight
  }
}

And define abstract class CodeTree as

abstract class CodeTree {
  def weight: Int
}

The reason for the error is that when you extend an class that take a parameter you need to supply the parameter. So for

abstract class CodeTree(weight: Int)

You need to extend it as

case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree(weight)

That's what the error you were getting was saying:

"not enough arguments for constructor CodeTree: (weight: Int)"

It was because you weren't supplying the required argument weight when extending CodeTree.

The problem with this approach though is that weight is not a member of CodeTree and hence can not to be accessed from an instance of type CodeTree. That is to say, if you did:

 scala> Fork(...).asInstanceOf[CodeTree].weight
 <console>:11: error: value weight is not a member of CodeTree

So, in your pattern match, you wouldn't be able to do x1.weight since the type of x1 is a CodeTree and CodeTree does not have a weight.

sk Bess

If you want to sort elements like codetrees, you can use Sorting.stableSort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!