Skip to content

Update the primary constructors proposal to give the body access to header parameters #4438

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

Merged
merged 5 commits into from
Jul 16, 2025

Conversation

eernstg
Copy link
Member

@eernstg eernstg commented Jul 4, 2025

This PR updates the primary constructors proposal to include support for access to header parameters in non-late instance variable initializers, and in general updates the static analysis and semantics to the new syntax introduced by #4428.

The approach relies on a very simple change to the scoping structure: The primary header parameters are added to a new scope which is the enclosing scope of the class body scope. This is completely standard according to the syntactic structure, just like the scoping around a function body and its formal parameter declarations.

This means that header parameters with a fresh name (that is, not clashing with the names declared in the class body) are in scope in the class body. It is an error to refer to them in any other location than an initializing expression of a non-late instance variable.

For the case where a name n in an initializing expression of a non-late instance variable is resolved to an instance variable declared by the class which has a corresponding primary header parameter (which is necessarily a declaring parameter with name n, or an initializing formal parameter with name n), it evaluates to the value of the parameter.

The difference between this approach and an approach where the primary header parameters are simply added to the scope of said initializing expressions is that we avoid allowing the given name to refer to two completely unrelated entities:

class A(final int x, int y) {
  String y;
  int z = x + y;
}

If we simply specify that the parameter int y is in scope for the initializing expression x + y then it all compiles and "works", but it is highly confusing that y does not refer to the instance variable named y, it refers to the parameter, and there is no relationship whatsoever between the parameter y and the instance variable y, they're just separate entities.

With this proposal it is a compile-time error for the initializing expression of z to refer to y: It can't refer to the instance variable because there is no access to this, and it can't use the "backup rule" that turns the evaluation of the instance variable into a parameter read (because that parameter has no relation to that instance variable). The point is that this error is useful because the situation is error prone.

In summary, the approach taken in this PR is to only allow an identifier expression like y to refer to a parameter if it resolves to an instance variable (according to the normal scope rules), and there is a corresponding primary header parameter. If there is no parameter with the given name, or there is a parameter but it doesn't correspond to the given instance variable, it's an error.

@eernstg eernstg requested a review from munificent July 4, 2025 11:17
@eernstg
Copy link
Member Author

eernstg commented Jul 4, 2025

@mraleph, how do you like this one? ;-)

@eernstg eernstg force-pushed the spec_primary_constructors_scope_jul25 branch from 1337f9a to b538a75 Compare July 9, 2025 08:26
Copy link
Member

@munificent munificent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@eernstg eernstg merged commit 4eefade into main Jul 16, 2025
4 checks passed
@eernstg eernstg deleted the spec_primary_constructors_scope_jul25 branch July 16, 2025 13:12
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.

2 participants