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

Means to optionally include a named argument #53764

Closed
lukehutch opened this issue Oct 16, 2023 · 2 comments
Closed

Means to optionally include a named argument #53764

lukehutch opened this issue Oct 16, 2023 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report

Comments

@lukehutch
Copy link

If a function makes a named parameter optional by specifying a default value, rather than by making it nullable, then there is no inline syntactical way of conditionally providing an argument to the parameter:

void x({int? y}) {}
void z({int y = 0}) {}

void baz(bool cond) {
  x();  // (1)
  z();  // (2)
  x(y: cond ? 10 : null);  // (3)
  if (cond) {  // (4)
    z(y: 10);
  } else {
    z();
  }

  // Does not work, but I wish it did:
  z(if (cond) y: 10);  // (5)
}

Both default values and a nullable type allow a named parameter not to be specified ((1) and (2)).

However, while you can conditionally provide an arg to a named parameter if it is nullable ((3)), you can't do it if a parameter is not nullable, even if it has a default value -- you have to use an if-statement ((4)).

It would be great if if-statements could be used to optionally provide named arguments ((5)). This is analogous to how if-statements can be used to optionally include elements in list literals.

@lrhn
Copy link
Member

lrhn commented Oct 20, 2023

That sounds like something like dart-lang/language#2316
I'd suggest using that issue for discussing more ideas.

@lrhn lrhn closed this as completed Oct 20, 2023
@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Oct 20, 2023
@lukehutch
Copy link
Author

Thanks, yes, it's the same request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

2 participants