Skip to content

Conversation

@eernstg
Copy link
Member

@eernstg eernstg commented Nov 5, 2025

The beginning of the 'private named parameters' proposal may currently be interpreted to mean that a declaring or initializing formal with a name of the form _id is treated as if it had had the name id for all purposes other than the naming of the implicitly induced instance variable. It is in fact still accessed as _id in the initializer list and (if not declaring or initializing) in the constructor body. This PR adds a few words to make it explicit that the corresponding public name is only used at call sites. It could be spelled out even more. However, that's probably not necessary because the details are given later in the proposal, and we just want to get the reader on track to the overall properties of the proposal here.

@munificent
Copy link
Member

Thanks!

@munificent
Copy link
Member

It is in fact still accessed as _id in the initializer list and (if declaring, not initializing) in the constructor body.

With that parenthetical, are you saying that a declaring field parameter is in scope in the constructor body? That's not what I'd expect:

class C({this._s}) {
  String? _s;
  void Function()? _capture;
  init {
    _capture = () {
      print(_s);
    }
  }
}

main() {
  var c = C(s: 'before');
  c._s = 'after';
  c._capture!.call();
}

I expect this program would print "after", meaning that _s in the constructor body refers to the instance variable, not the parameter. That's what a corresponding program using an initializing formal today does:

class C {
  String? s;
  void Function()? capture;
  C({this.s}) {
    capture = () {
      print(s);
    };
  }
}

main() {
  var c = C(s: 'before');
  c.s = 'after';
  c.capture!.call();
}

The primary constructors proposal says:

The same parameter list also introduces the primary parameter scope, whose enclosing scope is also the body scope of the class. Every primary parameter which is not declaring, not initializing, and not a super parameter is entered into this scope.

The primary parameter scope is the current scope for the body of the body part of the declaring header constructor, if any.

My emphasis added. So I think the proposal is in line with what I'm saying. Am I just misreading your parenthetical?

@eernstg
Copy link
Member Author

eernstg commented Nov 7, 2025

(if declaring, not initializing)

Oops, that was a typo. I just corrected the phrase to 'if not declaring or initializing'.

.. are you saying that a declaring field parameter is in scope in the constructor body? That's not what I'd expect

Right, the declaring parameter is not in scope in the body, it is treated in the same way as an initializing formal.

So I think the proposal is in line with what I'm saying.

Agreed!

@eernstg eernstg merged commit ad2369b into main Nov 7, 2025
4 checks passed
@eernstg eernstg deleted the spec_adjust_intro_nov25 branch November 7, 2025 10:59
Copy link

@Zhigan650 Zhigan650 left a comment

Choose a reason for hiding this comment

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

spec_adjust_intro_nov25

@Zhigan650
Copy link

@copilot ****

@Zhigan650
Copy link

Pull request successfully merged and closed

@Zhigan650
Copy link

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.

4 participants