
July 19th, 2010, 09:23 PM
|
|
Contributing User
|
|
Join Date: Nov 2008
Posts: 47
Time spent in forums: 7 h 4 m 52 sec
Reputation Power: 0
|
|
|
Scala case classes
Working with case classes, I'm not sure how the declaration for case class objects works
My code is below:
Code:
abstract class Tree[T]
case object Empty extends Tree
case class Binary[T](elem: T, left: Tree[T], right: Tree[T]) extends Tree[T]
...
def preOrder[T](t: Tree[T]):List[T] = t match{
case Empty => List()
case Binary(e, l, r) => List(e):::preOrder(l):::preOrder(r)
}
...
val test = Binary[Int](1, Empty, Empty)
But the declaration doesn't work. I know the logic for the preOrder method is right. I get an error when declaring Test that it requires a Tree[Int] for the second and third arguments, but Empty is a descendant of Tree. Changing the declaration of Empty to type T gives me this error:
type mismatch; found object p2.Empty required  2.Tree[T]
Thanks in advance.
|