No way to detect whether optional arg was passed #4180
Labels
area-language
Dart language related items (some items might be better tracked at github.com/dart-lang/language).
type-enhancement
A request for a change that isn't a bug
Milestone
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.
The text was updated successfully, but these errors were encountered: