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

No way to detect whether optional arg was passed #4180

Closed
DartBot opened this issue Jul 23, 2012 · 3 comments
Closed

No way to detect whether optional arg was passed #4180

DartBot opened this issue Jul 23, 2012 · 3 comments
Assignees
Milestone

Comments

@DartBot
Copy link

@DartBot DartBot commented Jul 23, 2012

This issue was originally filed by sammcca...@google.com


It'd be very nice to be able to detect whether an optional parameter was included in the argument list.

Checking against the default value (such as null) works in many cases but not all.

Motivation: I'm wrapping a web API where parameters are optional and nullable, e.g the following messages are all valid and distinct:

  {foo:true} // set foo to true
  {foo:false} // set foo to false
  {foo:null} // set foo to null
  {} // leave foo unchanged

This maps very naturally onto a Dart function call with optional parameters. (Note in a real example there may be >10 parameters)

  update(foo:true);
  update(foo:false);
  update(foo:null);
  update();

However this API is not easily implementable in Dart, as there's no way to query whether foo was passed.

e.g. if declared as update([bool foo]) then update() and update(foo:null) cannot be distinguished.

Workaround 1:
final UNDEFINED = // some constant object
update([bool foo=UNDEFINED]) { ... }

Problem: this doesn't work in checked mode, but crashes because UNDEFINED is not a bool.

Workaround 2:
final UNDEFINED = // some constant object
update([foo=UNDEFINED]) { ... }

Problem: this loses all type information in the API, which is unacceptable loss of documentation.

Workaround 3:
final UNDEFINED = // some constant object
abstract class Service {
  factory Service() => new ServiceImpl();
  update([bool foo]);
}
class ServiceImpl {
  update([foo = UNDEFINED]);
}

Problem: Large amount of boilerplate required - all method declarations have to be repeated twice, even those not affected by this problem.

@dgrove
Copy link
Member

@dgrove dgrove commented Jul 24, 2012

Set owner to @gbracha.
Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

@gbracha gbracha commented Jul 24, 2012

We plan to support this by M1, as part of a larger overhaul in the handling of optional parameters. Spec for this will be coming soon.


Added this to the M1 milestone.
Added Accepted label.

@gbracha
Copy link
Contributor

@gbracha gbracha commented Jul 30, 2012

In 0.11 draft.

See:

Analyzer issue #4264
VM issue #4265.
dart2js issue #4266.


Added Done label.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.