Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-6675

pattern matcher allows wrong number of subpatterns in extractor

    Details

      Description

      In addition to matching the contents of a tuple returned by `def unapply`, scala 2.10 also allows a single pattern matching the tuple itself. This is a regression from 2.9 in terms of pattern verification.

      Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
      Type in expressions to have them evaluated.
      Type :help for more information.
       
      scala> object X{def unapply(s: String):Option[(Int,Int,Int)]=Some((1,2,3))}
      defined module X
       
      scala> "" match { case X(b) => b }
      <console>:9: error: wrong number of arguments for object X
                    "" match { case X(b) => b }
                                     ^
      <console>:9: error: not found: value b
                    "" match { case X(b) => b }
                                            ^
      

      Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
      Type in expressions to have them evaluated.
      Type :help for more information.
       
      scala>  object X{def unapply(s: String):Option[(Int,Int,Int)]=Some((1,2,3))}
      defined module X
       
      scala> "" match { case X(b) => b }
      res0: (Int, Int, Int) = (1,2,3)
      

      This is problematic because there is no warning (except in certain lucky cases) if argument patterns are forgotten, resulting in a case statement which will never match, e.g. `case X(x: SomeTrait)`.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                moors Adriaan Moors
                Reporter:
                roland.kuhn Roland Kuhn
              • Votes:
                2 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: