Skip to content

How should default parameter values work with NNBD? #156

Closed
@leafpetersen

Description

@leafpetersen

We need to resolve how default parameter values should work with NNBD, for both nullably and non-nullably typed parameters.

Consider:

  void f({int x; int? y}) {...}
  void g({int x = 0; int? y = 1}) { }

Questions:

  • Is the declaration of x in f an error?
    • If not, then x is effectively required
      • This in turn means that if the type of f is a subtype of the type of g, then the x parameter of g must also be treated as required, which means there's no point in giving it a default.
      • So this effectively means that there are no optional non-nullable variables
      • Unless we separately added the notion of required named parameters that are part of the type
    • If so, then optional non-nullable parameters must always have a default value
  • Is passing null to the x parameter of g allowed (and interpreted the same as not passing anything)?
    • If so, then behaviorally, what's the difference between void Function({int x}) and void Function({int? x})? You can pass null to both of them.
  • Does passing null to the y argument of g bind y to null (current behavior) or to 1.
    • Changing this would be a breaking change.
    • This change has been advocated for independently, since it makes forwarding work better.

cc @munificent @lrhn @eernstg

Metadata

Metadata

Assignees

Labels

nnbdNNBD related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions