Skip to content

Lazy val with constant type loses side effects. #2266

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

Closed
nicolasstucki opened this issue Apr 15, 2017 · 3 comments · Fixed by #14853
Closed

Lazy val with constant type loses side effects. #2266

nicolasstucki opened this issue Apr 15, 2017 · 3 comments · Fixed by #14853
Assignees
Milestone

Comments

@nicolasstucki
Copy link
Contributor

lazy val x: true = { println("X"); true }
x

does not print X.

DarkDimius added a commit that referenced this issue Apr 19, 2017
Fix #2266: Do not replace constant type lazy vals with constant.
@felixmulder felixmulder reopened this Apr 26, 2017
@propensive propensive changed the title Lazy val with constant type looses side effects. Lazy val with constant type loses side effects. Apr 27, 2017
odersky added a commit that referenced this issue Apr 29, 2017
…vals

Revert "Fix #2266: Do not replace constant type lazy vals with constant."
@panacekcz
Copy link
Contributor

Should this issue be opened? Looks like it was closed automatically by a commit that reverted the fix.
The problem in general seems to be the same as in #4765, but in a different context.

@odersky
Copy link
Contributor

odersky commented Apr 5, 2022

scalac prints the "X", so this is a change in behavior that should be fixed.

@som-snytt
Copy link
Contributor

👍

Also, Scala 3 is also scalac. It is not some other thing.

odersky added a commit to dotty-staging/dotty that referenced this issue Apr 5, 2022
We previously converted an expression with constant type to a literal if
the expression was idempotent. This can hide side effects in the case where
the expression is a selection from an object or lazy val. Demanding purity
instead prodcues tons of errors involving inline vals on objects.

We now demand idempotency if the expression refers to an inline val (or
an operation over an inline val), and purity elsewhere.

Fixes scala#2266
odersky added a commit to dotty-staging/dotty that referenced this issue Apr 5, 2022
We previously converted an expression with constant type to a literal if
the expression was idempotent. This can hide side effects in the case where
the expression is a selection from an object or lazy val. Demanding purity
instead prodcues tons of errors involving inline vals on objects.

We now demand idempotency if the expression refers to an inline val (or
an operation over an inline val), and purity elsewhere.

Fixes scala#2266
@Kordyjan Kordyjan added this to the 3.2.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants