Skip to content

Commit 4eefade

Browse files
authored
Update the primary constructors proposal to give the body access to header parameters (#4438)
This commit 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: ```dart class A(final int x, int y) { String y; int z = x + y; } ``` If we 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 commit 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.
1 parent 2a305b0 commit 4eefade

File tree

1 file changed

+354
-141
lines changed

1 file changed

+354
-141
lines changed

0 commit comments

Comments
 (0)