Aladdin - Scala Bugtracking
[#903] project: compiler priority: low category: bug
submitter assigned to status date submitted
Nikolay Burak fixed 2007-01-19 10:06:44.0
subject [contrib #294] Problem using '@' with unapply pattern
code
class Person(_name : String, _father : Person) {
    def name = _name
    def father = _father
}

object PersonFather {
    def unapply(p : Person) : Option[Tuple1[Person]]  =
       if (p.father == null)
         None
       else
         Some(Tuple1(p.father))
}


object Test {
    def main(args : Array[String]) = {
       val p1 = new Person("p1",null)
       val p2 = new Person("p2",p1)

       p2 match {
          case aPerson@PersonFather(f) =>
              System.out.println(aPerson.name + "'s father is "+f.name);
          case _ => System.out.println("No father")
       }
     }
}
what happened
No compilation errors (compiled using -Xunapply), but the program fails at runtime with the following error:

$ scala -classpath . Test
java.lang.ClassCastException: scala.Tuple1
        at Test$.main(ExtrTest.scala:22)
        at Test.main(ExtrTest.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:76)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:100)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

what expected Expected to print: "p2's father is p1"
[back to overview]
Changes of this bug report
Nikolay  edited on  2007-01-19 10:12:55.0
contribution #294
Burak  edited on  2007-03-03 01:03:44.0