-
Notifications
You must be signed in to change notification settings - Fork 205
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
Wildcards and primary constructor syntax #3720
Comments
Disallowing using Allowing That leaves the question of whether it introduces a local variable in the constructor. That is: class C {
final int _;
C(this._) : assert(_ > 0);
} I would say that About naming of the parameter, if we introduce "public name of private initializer", that will, and can, only apply when removing the leading underscore leaves a valid identifier. So it wouldn't apply here, the parameter name is still What about function types? typedrf F = void Function(int _); is that allowed? void foo(int _) {} either, so it might even be accurate.) All in all, I'd treat a primary constructor parameter named And you can do C(super._, super._); to forward two positional parameters to a superclass constructor, and get no local variables in the initializer list. |
I think it's relevant to mention the proposal to allow an initializing formal to have a private name syntactically, and implicitly derive a public name which is the parameter name that is actually used at call sites: class A {
final int _i, _j;
A({this._i}) : _j = _i; // `_i` can be used in initializers.
}
void main() => A(i: 10); // The name is `i` at invocations. We discussed this in #2509, and #3058 is related as well. If we adopt this proposal then it would be an error to have an initializing formal parameter whose name is If we apply these rules to primary constructors (where the regular formal parameter declaration is desugared to an initializing formal and an instance variable declaration) then it would be an error to use This certainly makes sense to me for named parameters. With positional parameters it would actually make sense to say that (1) the syntactic parameter name So I'd recommend that we allow positional primary constructor parameters named |
Update: The adjustments I mentioned above have already been made in this proposal by @lrhn. The short version would then be: At this time it doesn't matter that a positional primary constructor parameter with syntactic name |
The spec side of this issue is handled in #3729. |
I was looking at a change to add semantic token modifiers to wildcards so that they could be styled differently by users (dart-lang/sdk#56567). While doing so, I found that the analyzer considered field formals to not be wildcards, but the spec seemed to imply that they could be:
While trying to find details about the rules here, I found @lrhn's comment above which says (emphasis mine):
So I'm a little confused about the rules. In particular, it's not clear to me what a "wildcard variable" exactly is, if it's not the same as something that doesn't introduce a name that can be referenced. If I wanted my IDE to color wildcard variables differently to non-wildcard variables, would the class C {
final int _;
C(this._);
} Thanks! |
The initializing formal is an ambiguous case: Usually, the name of an initializing formal can be used to denote the formal parameter in the initializer list: class A {
final int x, y;
A(this.x): y = x + 1;
} However, we can use an initializing formal of the form class A {
final int _, y;
A(this._): y = _ + 1; // Error, cannot access `this._` here.
} It can be argued that the initializing formal declaration should be highlighted as a wildcard (because the parameter doesn't introduce a name into the scope where this normally goes), and also that it should be highlighted as an identifier, because it's clearly associated with the instance variable with that name. I don't know what's more convincing. Semi-transparent? ;-) |
I guess I was more interested in the definition than how this applies specifically to colouring. The analyzer has an existing extension getter The current behaviour works fine for me (they would not be coloured as wildcards, and I don't think this would be confusing to anyone), but it made me wonder if the definition of @bwilkerson given the info above, do you think there may be any issue here, or does excluding field formals in |
I took a brief look at the list linked above, and all of those uses of the extension getter seem reasonable to me. I think we shouldn't highlight an occurrence of |
@bwilkerson sgtm, thanks! |
There are no wildcard variables. The entire point of the "unnamed variables" feature is to not actually introduce a variable. A "wildcard variable declaration", or more precisely an unnamed variable declaration is a declaration which looks like a variable declaration with a declared name of Obviously we're being imprecise about this and omitting the "declaration" all the time. We probably don't even agree on the underlying model, just the visible behavior, so this is, like, my opinion. One could argue that it does introduce a variable, that variable just doesn't introduce a name into the surrounding scope, so there is no way to refer to the variable. An initializing formal like The syntax is two things at the same time. An initializing formal for an instance variable named If the I'd probably want to colorize |
I think this is the intention, and I think for colouring we should use this same definition.
Yep, this is the case today, terms like Thanks for the insights, they've been very helpful! :-) |
Trying to nail down the last few cases of wildcards. I'll regurgitate the issues and options that @munificent wrote to me and bring it up for discussion:
Options:
Yes, the wildcard proposal applies and it gets no name that you can use. Probably not very useful, but consistent with the syntax which does look like a parameter declaration.
No, the wildcard proposal does not apply since you're defining a field. You get a field named
_
. Probably useful.Avoid the situation entirely by making it an error to have a parameter named
_
in a primary constructor or extension type.@dart-lang/language-team It would be good to make a decision on this and pick an outcome. Your input please?
The text was updated successfully, but these errors were encountered: