Update the primary constructors proposal to give the body access to header parameters #4438
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 namen
, or an initializing formal parameter with namen
), 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:
If we simply specify that the parameter
int y
is in scope for the initializing expressionx + y
then it all compiles and "works", but it is highly confusing thaty
does not refer to the instance variable namedy
, it refers to the parameter, and there is no relationship whatsoever between the parametery
and the instance variabley
, they're just separate entities.With this proposal it is a compile-time error for the initializing expression of
z
to refer toy
: It can't refer to the instance variable because there is no access tothis
, 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.