Scala 解决讨厌的模式匹配错误
我正在定义一个通用的树结构,即一个可以扩展的树结构,例如分支和叶子包含额外的值(例如,我需要它来添加一个名称字符串) 看起来是这样的:Scala 解决讨厌的模式匹配错误,scala,types,pattern-matching,Scala,Types,Pattern Matching,我正在定义一个通用的树结构,即一个可以扩展的树结构,例如分支和叶子包含额外的值(例如,我需要它来添加一个名称字符串) 看起来是这样的: trait TreeNodes { val Node = Either type Node[+B, +L] = Either[B, L] val IsBranch = Left type IsBranch[+B, +L] = Left[B, L] val IsLeaf
trait TreeNodes {
val Node = Either
type Node[+B, +L] = Either[B, L]
val IsBranch = Left
type IsBranch[+B, +L] = Left[B, L]
val IsLeaf = Right
type IsLeaf[+B, +L] = Right[B, L]
}
object TreeLike extends TreeNodes {
trait BranchLike[Elem, B, L] {
type N = Node[B, L]
def iterator: Iterator[N]
}
trait LeafLike[Elem] {
def value: Elem
}
}
trait TreeLike[Elem, Repr] {
type Leaf <: TreeLike.LeafLike[Elem]
type Branch <: TreeLike.BranchLike[Elem, Branch, Leaf]
def root: Branch
}
我怀疑patmat在T分支上有问题。你知道怎么在这里工作吗
我对在
中包装树叶和树枝也不是100%满意。这是必要的,因为当我试图定义一个超级类型的LeafLike
和BranchLike
并找出如何在实现中正确地子类型时,这些东西就失控了,模式匹配也失败了,因为我没有找到正确的提取器。因此,使用或者不是一个坏主意吗?类型别名N
是一个“问题”(WTF)
这使得它可以编译。只是,如果这座建筑即将倒塌,我就没有动力继续建造它:-(使用Scala 2.11.0-M6时编译器不会崩溃……我想你看到了,这条消息中有一长串错误。:/@S.R.I很有趣……不过,这张罚单并没有说它在2.11.0-M6中得到了解决。我可能应该在这里附上我的示例。是的,这个错误似乎对编译器造成了全面的打击,这是一个太多人的沼泽地y-场景。只需搜索“无符号无所有者”即可查看有多少脚踩过该路径。:)
def test[Elem, T <: TreeLike[Elem, T]](tree: T): Unit = {
import TreeLike.{IsLeaf, IsBranch}
def printLeaves(b: T#Branch): Unit = b.iterator.foreach {
case IsLeaf(l) => println(l.value)
case IsBranch(c) => printLeaves(c)
}
printLeaves(tree.root)
}
[error] during phase: patmat
[error] library version: version 2.10.3
[error] compiler version: version 2.10.3
...
[error] symbol definition: case val x1: b.N
[error] tpe: b.N
[error] symbol owners: value x1
[error] context owners: value x0$1 -> value $anonfun -> method printLeaves ->
method test -> object Voodoo -> package typerbug
...
[error] no-symbol does not have an owner
trait BranchLike[Elem, B, L] {
def iterator: Iterator[Node[B, L]]
}