What is the result of executing the following code?
+
+sealed trait obj
+case object one extends obj
+case object two extends obj
+def test(o: obj) =
+ o match {
+ case one ⇒ 1
+ case two ⇒ 2
+ }
+println(test(one))
+println(test(two))
+
+
+
+
prints "1" and then "2"
+
throws MatchError
+
prints "1" and then "1"
+
compile error
+
+
+
+
+
Explanation
+
+ Identifiers that start with a lower-case letter are treated as a new variable with that name, whose value is the
+ input to the match expression.
+
+ Generally, you don't want to do this unless it's the last, "catch-all" case.
+
+ To correctly match on the objects above, you'd have to do:
+
+
+def ok(o: obj) =
+ o match {
+ case _: one.type ⇒ 1
+ case _: two.type ⇒ 2
+ }
+