I'm trying out the code at http://www.scala-lang.org/node/112 and I'm getting a match error for something that doesn't look like it should throw one.
This is the original code:
object Twice {
def apply(x: Int): Int = x * 2
def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None
}
object TwiceTest extends Application {
val x = Twice(21)
x match { case Twice(n) => Console.println(n) } // prints 21
}
I just added a few lines to test what happens when I pass an odd number:
object TwiceTest extends Application {
val x = Twice(21)
x match { case Twice(n) => Console.println(n) } // prints 21
val y = 21
y match { case Twice(n) => Console.println(n) } // throws scala.MatchError: 21 (of class java.lang.Integer)
}
The case for 21 or any odd number should also be handled by the unapply method in the object as far as I can tell. Can someone explain why this is not the case?