Scala Programming Language
  1. Scala Programming Language
  2. SI-900

Allow pattern matching on type in for comprehensions

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Pattern Matcher
    • Labels:
      None

      Description

      It would be nice if pattern matches on type worked inside for comprehensions:

      scala> for (Some(x) <- List(None, Some(1))) yield x
      res4: List[Int] = List(1)
      
      scala> for (x : Some[_] <- List(None, Some(1))) yield x
      <console>:5: error: type mismatch;
       found   : (Some[_]) => Some[Any]
       required: (Option[Int]) => ?
             for (x : Some[_] <- List(None, Some(1))) yield x
                  ^
      scala> Some(1) match { case x : Some[_] => x }
      res7: Some[Any] = Some(1)
      

      I'd like the middle one to be a pattern match on type, so instead of a type error it would give List(Some(1)).

        Issue Links

          Activity

          Hide
          Piotr Czapla added a comment -

          I"ve just got bitten by this. Your book does not seems to mention about this problem either. And there is almost no info on the net about this. Fortunately folks at SO helped me: http://stackoverflow.com/questions/11394034/why-scalas-pattern-maching-does-not-work-in-for-loops-for-type-matching

          Anyway +1 to fix this.

          Show
          Piotr Czapla added a comment - I"ve just got bitten by this. Your book does not seems to mention about this problem either. And there is almost no info on the net about this. Fortunately folks at SO helped me: http://stackoverflow.com/questions/11394034/why-scalas-pattern-maching-does-not-work-in-for-loops-for-type-matching Anyway +1 to fix this.
          Hide
          Ben Challenor added a comment -

          Another +1 for this fix. Thanks.

          Show
          Ben Challenor added a comment - Another +1 for this fix. Thanks.
          Hide
          Max Bolingbroke added a comment -

          This is rather surprising behavior and seems to contradict the spec. It would be great if this could be fixed.

          Show
          Max Bolingbroke added a comment - This is rather surprising behavior and seems to contradict the spec. It would be great if this could be fixed.
          Show
          Denys Shabalin added a comment - - edited Fix https://github.com/densh/scala/commit/afd03a19db1abb229ec878b3b1868279e912aeaf
          Hide
          Denys Shabalin added a comment -

          After discussion today the conclusion was that this behavior will not change in 2.11 but should be discussed again in 2.12 cycle.

          Show
          Denys Shabalin added a comment - After discussion today the conclusion was that this behavior will not change in 2.11 but should be discussed again in 2.12 cycle.

            People

            • Assignee:
              Martin Odersky
              Reporter:
              David R. MacIver
              TracCC:
              Daniel Sobral, jpalecek, Mikhail Vorozhtsov, Rafael de F. Ferreira, Seth Tisue
            • Votes:
              9 Vote for this issue
              Watchers:
              16 Start watching this issue

              Dates

              • Created:
                Updated:

                Development