Skip to content
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

(Analyzer, package:meta) Annotations on field initializers #27570

Closed
matanlurey opened this issue Oct 11, 2016 · 3 comments
Closed

(Analyzer, package:meta) Annotations on field initializers #27570

matanlurey opened this issue Oct 11, 2016 · 3 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@matanlurey
Copy link
Contributor

I'd like to be able to write

class Foo {
  @required
  final String name;

  const Foo({this.name});
}

Instead of

class Foo {
  final String name;

  const Foo({@required this.name});
}

Also see dart-lang/dartdoc#1259

@bwilkerson bwilkerson added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Oct 11, 2016
@bwilkerson
Copy link
Member

I'm not certain I understand the semantics you're asking for.

Would this only apply to final fields, or can it be applied to any instance field? I assume it wouldn't apply to static fields.

Would this also imply that every constructor is required to have a named field formal parameter for the field and that we would produce a hint if (a) there is no constructor or (b) there is a constructor that doesn't have it as a named parameter? What if it were a normal (required) parameter for some constructor? I assume that it wouldn't make sense for it to be an optional positional parameter.

@matanlurey
Copy link
Contributor Author

No no, not required, this would be to supplement existing work.

What I'm asking for is if you have a field formal parameter, the annotation could be on the field instead of the parameter.

@bwilkerson
Copy link
Member

The shorthand seems convenient, but I think there is a significant possibility that allowing this annotation on fields would confuse users more than it helps them. That's why I'm asking about the semantics, in case you have a semantic that makes sense.

As a thought experiment, assume that this annotation doesn't exist.

If I tell a user (verbally, for example) that the parameter p (of some constructor) is required, then they know what that means because we talk about required parameters in other contexts: it means that when they invoke the constructor they need to provide a value for p. If p is a named parameter, then they are either confused (because the spec says named parameters are optional) or they figure out that it means that it is not optional despite being named (that we're working around a limitation in the language, that it won't let us define parameters that are both named and required).

If I tell a user that an instance field f is required, what does that imply to them? I don't think they have a context for understanding that, and I don't think their first assumption will be that it only applies to references to the field in named constructor parameters (because it looks like it applies to the field, not a specific kind of reference to the field).

Now assume that the annotation exists and is allowed on fields. What if the user saw code like the following:

class C {
  @required
  int f;

  C([this.f]);
}

Is that parameter required? If so, then why it is allowed to be marked as optional? If not, why not?

I think that if we allow the annotation on fields then we need to define the semantics, and that includes issuing hints if it's used in a way that doesn't make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants