First steps
Let's define the list nodes. First, let's briefly look at a sealed trait
. A trait
is just an interface. It may contain methods too. See http://joelabrahamsson.com/learning-scala-part-seven-traits/
for more information.
The sealed
keyword allows the compiler to do exhaustive checking. For example, here is an REPL session to see this feature in action:
scala> trait A defined trait A scala> case class B() extends A defined class B scala> case class C() extends A defined class C scala> def m(a: A) = a match { | case B() => println("It is a B") | }
Note the definition of the m(A)
method. It just handles objects of this type: B
.
What happens when we call the m(C())
method? We get a match error:
scala> m(C()) scala.MatchError:..
The sealed
keyword helps in this case. Just change the definition of trait A
as follows:
sealed trait A
You also need to redefine the B
and C
case classes. Just repeating the definition should be...