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 declaring, not 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?

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.

3 participants