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?
is the same as
meaning that
xwill be equal to42. TheTwice.unapply(42)returns aSome(21), meaning that thecase Twice(21)successfully matches the valuex == 42.This is why the first
matchstatement prints out21.The
Twice.unapply(21)returnsNone(becausey == 21, that is, ifyis odd).Whenever an
unapplyreturnsNonefor some value, we say that the extractor object with thatunapplymethod does not match that value.If a
matchstatement does not match a value to any of its cases, it will throw aMatchError.