Skip to content

Allow both optional positional and optional named arguments in the same function signature. #1076

@lrhn

Description

@lrhn
Member

Dart functions currently can have only either optional named parameters or optional positional parameters, but not both.

While developing libraries repeatedly run into cases where this restriction gets in the way of the signature we want.
It would be preferable if you could have both kinds of parameters on the same function.
The only syntax change necessary is to the declaration of functions. The call syntax is unaffected, and so is noSuchMethod/Function.apply.

Activity

floitschG

floitschG commented on Nov 30, 2012

@floitschG

This issue is related to issue dart-lang/sdk#6496, but asks for less.

DartBot

DartBot commented on Nov 30, 2012

@DartBot

This comment was originally written by @seaneagan


Is this asking for:

  • 2 separate optional parameters within the same declaration, one named, one positional
  • allowing a single optional parameter to be both named and positional

The former could be solved by non-overlapping [] and {}. If overlapping [] and {} are allowed, then would have to decide whether to allow both {[ and [{ as well as }] and ]].

floitschG

floitschG commented on Nov 30, 2012

@floitschG

It is asking for non-overlapping [] and {}.
Examples:
new List([length = 0], {fill: null});
Stream.subscribe([onData], {onError, onDone})
new Date(year, [month, ...., milliseconds], {isUtc: false})

gbracha

gbracha commented on Nov 30, 2012

@gbracha

Added this to the Later milestone.
Added Accepted label.

DartBot

DartBot commented on Dec 1, 2012

@DartBot

This comment was originally written by @seaneagan


Sounds perfect! The only other thing I would change (also mentioned in issue dart-lang/sdk#6496) is to use = instead of : for named positional default values:

new List([length = 0], {fill = null});

The = to default positional optionals doesn't mimic call site syntax, so why does the named positional default syntax try to? I think it's more importatnt to be consistent between how to default optional parameters regardless of whether they are named or positional.

DartBot

DartBot commented on Feb 25, 2014

@DartBot

This comment was originally written by @rbishop-bah


Related: Issue dart-lang/sdk#17101

kasperl

kasperl commented on Jul 10, 2014

@kasperl

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

kasperl

kasperl commented on Aug 4, 2014

@kasperl
gbracha

gbracha commented on Aug 27, 2014

@gbracha

Issue dart-lang/sdk#17101 has been merged into this issue.

lrhn

lrhn commented on Oct 28, 2014

@lrhn
MemberAuthor

Marked this as blocking dart-lang/sdk#21406.

donny-dont

donny-dont commented on Sep 9, 2015

@donny-dont

Since 2.0 is thinking of some drastic changes is there any way the more extreme do like C#, Python, of dart-lang/sdk#6496 can be revisited.

@gbracha would a DEP be required for this?

seaneagan

seaneagan commented on Sep 10, 2015

@seaneagan

@donny-dont if this issue is fixed then we will be able to do:

greet([String salutation = 'Hello'], {String who}) {
  print('$salutation $who!');
}

Then if there is really a need to allow parameters to be both named and positional, we could further allow something like this syntax:

greet({[String salutation = 'Hello', String who]}) {
  print('$salutation $who!');
}

But personally, I think that creates unnecessarily bloated API surface.

bobjackman

bobjackman commented on Sep 29, 2017

@bobjackman

Any guesses when this might get implemented?

69 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    requestRequests to resolve a particular developer problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @munificent@wmleler@seaneagan@PrimaryFeather@donny-dont

        Issue actions

          Allow both optional positional and optional named arguments in the same function signature. · Issue #1076 · dart-lang/language