I am a newbie in Scala and I want to build a small recursive pattern matching with objects.
I keep getting a No match in the following code:
abstract class FixedNode
case class LoopBeginNode(lb: ArrayLengthNode) extends FixedNode
case class ArrayLengthNode(al: FixedNode) extends FixedNode
case class IfNode(i: FixedNode) extends FixedNode
case class BeginNode(b: FixedNode) extends FixedNode
case class LoadIndexedNode(li: FixedNode) extends FixedNode
case class FixedGuardNode(fg: FixedNode) extends FixedNode
case class EndNode(e: String) extends FixedNode
object HelloWorld {
def main(args: Array[String]) {
println(matchIt(LoopBeginNode(ArrayLengthNode(EndNode("")))))
}
def matchIt(e: FixedNode): String = e match {
case EndNode(en) => "match"
case LoopBeginNode(lb) if lb == ArrayLengthNode(e) => matchIt(lb)
case ArrayLengthNode(al) if al == EndNode("") => matchIt(al)
case IfNode(i) if i == BeginNode(i) => matchIt(i)
case BeginNode(b) if b == EndNode("") =>matchIt(b)
case _ => "No match"
}
}
If I add a simple LoopBeginNode(EndNode(""))
I get a match. Although in the more complex case above I get a mismatch, which according to my understanding, comes from the lb
variable which is an EndNode
whereas in the condition is a FixedNode
. Do you have any idea how I am going to make it work?
I want in the end to conclude in something more complex like
ArrayLengthNode(IfNode(BeginNode(LoadIndexedNode(GuardeElseNode(LoadIndexNode(IfNode(EndNode(""))))))))
question from:
https://stackoverflow.com/questions/65924782/scala-match-recursive-objects 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…