Skip to content

No way to detect whether optional arg was passed #4180

Closed
@DartBot

Description

@DartBot

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.

Activity

dgrove

dgrove commented on Jul 24, 2012

@dgrove
Contributor

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

gbracha

gbracha commented on Jul 24, 2012

@gbracha
Contributor

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

gbracha commented on Jul 30, 2012

@gbracha
Contributor

In 0.11 draft.

See:

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


Added Done label.

added this to the M1 milestone on Jul 30, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @kevmoo@dgrove@gbracha@DartBot@mit-mit

      Issue actions

        No way to detect whether optional arg was passed · Issue #4180 · dart-lang/sdk