我有一个节点类,它可以指向另一个节点(通过next
)。该节点可以分为不同的层次结构。然后,我可以在单链表中的图像中获得这些不同节点的链。然后,从该列表中的任意节点开始,我想搜索特定类型的第一个节点。因此,我为此创建了一个类型参数化函数check
。但它找不到正确的匹配。下面是完整的输出代码示例,从斯卡拉底稿获得:Scala - 在递归函数中匹配传递的参数类型
class A (i: Int) {
var next: Option[A] = None
def + (a: A) = {
next = Some(a)
a
}
override def toString = super.toString + "[" + i + "]"
}
class B (i: Int) extends A (i)
class C (i: Int) extends B (i)
class D (i: Int) extends B (i)
object test {
val start = (new A(0)) //> start : A = [email protected][0]
val test = start + (new A(1)) + (new B(2)) + (new C(3)) + (new D(4)) + (new A(5)) + (new C(6))
//> test : A = [email protected][6]
def check[T <: B](a: A): Option[B] = {
println("starting search for " + a)
a match {
case t: T =>
println("SUCCESS, found=" + t)
Some(t)
case wrong =>
println("did not find! wrong=" + wrong)
a.next match {
case Some(nxt) =>
println("checking next=" + nxt)
check[T](nxt)
case _ =>
println("SEARCH FAILED")
None
}
}
} //> check: [T <: B](a: A)Option[B]
/* correct - i expect this */
println(check[C](new A(1))) //> starting search for [email protected][1]
//| did not find! [email protected][1]
//| SEARCH FAILED
//| None
/* correct - i expect this */
println(check[D](new A(2))) //> starting search for [email protected][2]
//| did not find! [email protected][2]
//| SEARCH FAILED
//| None
/* incorrect - it must fail looking for C */
println(check[C](new B(4))) //> starting search for [email protected][4]
//| SUCCESS, [email protected][4]
//| Some([email protected][4])
println(check[B](start)) //> starting search for [email protected][0]
//| did not find! [email protected][0]
//| checking [email protected][1]
//| starting search for [email protected]fe64b9[1]
//| did not find! [email protected][1]
//| checking [email protected][2]
//| starting search for [email protected][2]
//| SUCCESS, [email protected][2]
//| Some([email protected][2])
/* incorrect - it must find C(3) instead */
println(check[C](start)) //> starting search for [email protected][0]
//| did not find! [email protected][0]
//| checking [email protected][1]
//| starting search for [email protected][1]
//| did not find! [email protected][1]
//| checking [email protected][2]
//| starting search for [email protected][2]
//| SUCCESS, [email protected][2]
//| Some([email protected][2])
/* incorrect - it must find D(4) instead */
println(check[D](start)) //> starting search for [email protected][0]
//| did not find! [email protected][0]
//| checking [email protected][1]
//| starting search for [email protected][1]
//| did not find! [email protected][1]
//| checking [email protected][2]
//| starting search for [email protected][2]
//| SUCCESS, [email protected][2]
//| Some([email protected][2])
的问题是:
为什么会失败这样呢?它忽视了传入的类型。
我该如何实现我想要的?
UPDATE:作为谏,我曾为了争夺所谓的“类型擦除”在这里,这样做手工明确的具体化使用Scala的“清单”尝试。然而,我无法在清单的帮助下进行模式匹配 - 它无法以我能想到的任何方式工作,也无法在网络上找到任何工作示例。
是的,没有一个解释downvoting - 这是怎样的一些表现自己的深厚造诣。 – noncom
在您的投诉中使用“manifest”这个词很有趣。也许你应该谷歌。 –
@KimStebel,是的,我认为是。我用Google搜索了一下,并且研究了这个话题,我可以怎样处理这个话题,并且用我所能想到的所有可能的方式,使用清单,我无法实现正确的匹配。我甚至尝试用传递的类型和参数的清单的不同关系替换匹配if-condition,但无法完成。你能否提供一点关于如何正确使用它的提示?我有点失落..清单似乎是我遇到过的最深奥的样板之一...... – noncom