Well, I run your code through the interpreter and I think it has nothing to do with type erasure. May be just some syntax error. Try this:
object Visitor {
def inorder[T](root: Node[T]): Unit = {
root match {
case End => return;
case n:Node[_] => {
if( root.left != null )
inorder( root.left )
println( root.toString );
inorder( root.right );
}
case _ => return;
}
}
}
You need to indicate the variable name that will be bound to the match, like n:Node[T]
. That would give you a warning about type erasure, which you can remove by using n:Node[_]
. The compiler can infer the type of root.left
and root.right
based on the type of root
so type erasure doesn't really come into play here...
Note: I just checked the spec, the syntax for a type pattern is:
varid ':' TypePat
'_' ':' TypePat
It would help if you provide the actual error message from the compiler, and the definition of Node and End as otherwise we need to infer all those things.
Edit:
Assuming you're compiling http://aperiodic.net/phil/scala/s-99/tree.scala, there is indeed a syntax error in your code. Use n:Node[_]
. Then you'll get some type errors. Here is what works for me:
import binarytree._
object Visitor {
def inorder[T](root: Tree[T]) : Unit = {
root match {
case End => return;
case n:Node[_] => {
if( n.left != null )
inorder( n.left )
println( n.toString );
inorder( n.right );
}
case _ => return;
}
}
}