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

Desugar lazy val pattern bindings into lazy vals #18879

Closed
wants to merge 1 commit into from

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Nov 8, 2023

Fixes #18878.

Reverts change in 54f6399.

Example:

lazy val (a, b) = ...

desugars into

private lazy val t$1 = ...
lazy val a = t$1._1 // before: def a = t$1._1
lazy val b = t$1._2 // before: def b = t$1._2

Where a and b should be stable identifiers.

Fixes scala#18878

Reverts change in 54f6399

Example:
```
lazy val (a, b) = ...
```
desugars into
```
private lazy val t$1 = ...
lazy val a = t$1._1 // before: def a = t$1._1
lazy val b = t$1._2 // before: def b = t$1._2
```
Where `a` and `b` should be stable identifiers.
@nicolasstucki nicolasstucki self-assigned this Nov 8, 2023
@nicolasstucki
Copy link
Contributor Author

An alternative would be to introduce the internal concept of a lazy def. This would be used to indicate an accessor to a stable lazy value. This would have larger implications for the language and compiler.

@odersky
Copy link
Contributor

odersky commented Nov 8, 2023

I think the change to defs was done very expressedly since val's would not be sound. Mapping to lazy vals would be very expensive (if they are local they lead to boxing), so I think this is not a good fix either.

It's best to leave things as they are, or tighten the screws further. A lazy val is not stable, since it is not a constructed value. We can get all the famous unsoundness tricks by replacing vals with lazy vals. So I think the most logical way forward is to declare that a.type is valid only for stable a, and therefore to disallow this:

  lazy val a = "bbb"
  val b: a.type = a

@sjrd sjrd deleted the fix-18878 branch November 8, 2023 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-stable lazy val pattern bindings
2 participants