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

remove the requirement for required on nnbd arguments #2050

Closed
bsutton opened this issue Jan 5, 2022 · 3 comments
Closed

remove the requirement for required on nnbd arguments #2050

bsutton opened this issue Jan 5, 2022 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@bsutton
Copy link

bsutton commented Jan 5, 2022

When defining a named argument with a non-nullable type and no default you are required to add the required keyword.

This appears to be redundant.

From the diagnostics page:

missing_default_value_for_parameter
The parameter ‘{0}’ can’t have a value of ‘null’ because of its type, but the implicit default value is ‘null’.

Description
The analyzer produces this diagnostic when an optional parameter, whether positional or named, has a potentially non-nullable type and doesn’t specify a default value. Optional parameters that have no explicit default value have an implicit default value of null. If the type of the parameter doesn’t allow the parameter to have a value of null, then the implicit default value isn’t valid.

I've identified five use patterns for named arguments:

abstract class test {
  void one({required String name});

  void two({String name = 'hi'});

  void three({String? name});

  void four({String? name = 'hi'});

  void five({required String? name});
}

The option for required with pattern five makes sense, the requirement on one appears to be redundant.

Given most of my usage of named arguments is for non-nullable types this redundant requirement is somewhat annoying.

It would appear that we could remove this requirement and provide a lint to advise that it can be removed.

I don't think this would require any migration as it simply becomes optional.

@bsutton bsutton added the feature Proposed language feature that solves one or more problems label Jan 5, 2022
@mateusfccp
Copy link
Contributor

We had a discussion about this a while ago in #878. Unfortunately, it was closed, but this makes complete sense.

@leafpetersen
Copy link
Member

This has been very extensively discussed, both in #878 and in the original design issue thread #156 (see this comment for pointers to some of the most relevant bits). I'm going to close this out, unless there's something new to discuss here (in which case it might be better to tack it on to #878 where there's already context.

@bsutton
Copy link
Author

bsutton commented Jan 5, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants