Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scabug opened this issue Mar 17, 2012 · 3 comments
Closed

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

scabug opened this issue Mar 17, 2012 · 3 comments
Assignees

Comments

@scabug
Copy link

@scabug scabug commented Mar 17, 2012

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
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Mar 17, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5582?orig=1
Reporter: Tomas Shestakov (megaprog)
Affected Versions: 2.9.1

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Mar 18, 2012

@soc said:
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.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Mar 18, 2012

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

@scabug scabug closed this Mar 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.