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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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!

@@ -60,6 +60,12 @@ more concisely:
class Point(var int x, var int y);
```

A class that has a primary header constructor can not have any other
Copy link
Member

Choose a reason for hiding this comment

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

Somewhat idle thought: With an in-body primary constructor, there may be other generative constructors that don't redirect to it, so it's not really "primary" in the same sense that it is in Scala and Kotlin. That suggests that "primary" isn't a good name for these.

With an in-header primary constructor... there are no other kinds of constructors you can put in the header. That suggests that either or "in-header" or "primary" is redundant. In this case, the constructor is primary: all other generative constructors must redirect to it.

I wonder if we should rename things to "declaring constructor" for an in-body primary and "primary constructor" for an in-header primary?

Alternatively, we could say that if a class has an in-body primary constructor then all other generative constructors must redirect to it. That restriction isn't strictly necessary for correctness, but might make it easier for users to understand how instances of the class get initialized. In that case, the current terminology makes sense.

the syntax `var name`, it must have a type (`T name`) or the type must be
omitted (`name`).
Similarly, with this feature a regular (non-declaring) formal parameter can
not be declared with the syntax `var name`, it must have a type (`T name`)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: "be declared with" -> "use".

It's a little confusing to have "declaring" and "declared" in the same sentence while referring to different concepts.

@@ -382,6 +388,46 @@ avoiding the duplication of instance variable names and types) even in the
case where some parameters should not introduce any instance variables (so
they are just "normal" parameters).

With a primary header constructor, the formal parameters in the header are
introduced into a new scope. This means that the parameters whose name is
not introduced by any nested scope (e.g., the class body scope) is in scope
Copy link
Member

Choose a reason for hiding this comment

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

"is" -> "are" in both places. Or "the parameters" -> "a parameter".


*Note that it only applies to identifier expressions. In particular, this
does not allow initializing expressions to assign to other instance
variables.*
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to spell out how this behaves with respect to capture:

class C(var int x) {
  var closure = () => x;
}

main() {
  var c = C(1);
  c.x = 2;
  print(c.closure()); // Prints 1.
}

@@ -675,7 +790,8 @@ of the fact that the primary constructor is actually placed outside the
braces that delimit the class body.*

Next, _k_ has the modifier `const` iff the keyword `const` occurs just
before the name of _D_ or before `this`, or _D_ is an `enum` declaration.
before the name of _D_ or before `this`, or if _D_ is an `enum`
declaration.
Copy link
Member

Choose a reason for hiding this comment

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

So we infer const for header constructors on enums, but not body constructors? That seems like an irregularity.

A warning is emitted in the case where an identifier expression is resolved
to yield the value of a declaring or initializing formal parameter in a
primary header constructor, and this identifier occurs in a function
literal, and the corresponding instance variable is non-final.
Copy link
Member

Choose a reason for hiding this comment

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

An example would help clarify here. :)


The language does not specify warnings, but the following is recommended:

A warning is emitted in the case where an identifier expression is resolved
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just make this an error. I can't imagine it every doing anything useful and will certainly just confuse users.

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