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

Why `for (i <- 1 to 10) { s } += i` compile and work ?

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Not a Bug
    • Affects Version/s: Scala 2.9.1
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      Windows 7, IntelliJ IDEA 11.0.2, JDK 1.7.03

      Description

      Why this code compile and work?

      object TestForResults extends App {
        var s = 0
        val a = for (i <- 1 to 10) { s } += i
        println(a)
        println(s)
      }
      

      Output will be:

      ()
      55
      

        Activity

        Hide
        Simon Ochsenreither added a comment -

        Hi Thomas,

        this should have been asked on the mailing list first, probably.

        Maybe it makes it a bit more clear, this code is equivalent:

          for (i <- 1 to 10) s += i
        

        The `{}` is not doing what you expect.

        Show
        Simon Ochsenreither added a comment - Hi Thomas, this should have been asked on the mailing list first, probably. Maybe it makes it a bit more clear, this code is equivalent: for (i <- 1 to 10) s += i The `{}` is not doing what you expect.
        Hide
        Paul Phillips added a comment -

        I guess "why does this work" has some novelty value. Yes, please don't open bugs without some evidence there is a bug.

        Show
        Paul Phillips added a comment - I guess "why does this work" has some novelty value. Yes, please don't open bugs without some evidence there is a bug.

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Tomas Shestakov
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development