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

Add "Spread operator" #7023

Closed
DartBot opened this issue Nov 29, 2012 · 10 comments
Closed

Add "Spread operator" #7023

DartBot opened this issue Nov 29, 2012 · 10 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 29, 2012

This issue was originally filed by guichard.ad...@gmail.com


I did not find any informations about this operator during my Dart trip :

The spread operator allows to invoke an action on all items of an aggregate object. It is equivalent to calling the collect method like so:

(translated in Dart from Groovy exemple)
parent*.action; //equivalent to:
parent.forEach((child) { child?.action; });

or
List<Parent> parents;
[. . .]
List<String> parentNames = parents*.name

Groovy page discribing this operator:
http://groovy.codehaus.org/Operators

Thanks for your support,
Adrien

@anders-sandholm
Copy link
Contributor

Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Aug 25, 2014

Set owner to @gbracha.
Added Accepted label.

@DartBot
Copy link
Author

DartBot commented Jan 25, 2015

This comment was originally written by @kaendfinger


This is very useful, I used to be a huge user of Groovy, so I know how it feels to use them. In future versions of the spec, this would a great addition!

@DartBot
Copy link
Author

DartBot commented Jan 26, 2015

This comment was originally written by @seaneagan


So:

  parents*.name

would be equivalent to:

  parents.map((parent) => parent == null ? null : parent.name);

I think more useful would be having a way of closurizing instance methods from a class instead of instance, then if issue #41 is fixed adding a "?" option to that, so something like:

  parents.map(Parent?.name)

  class Parent {
    String get name;
  }

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jan 26, 2015
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@munificent
Copy link
Member

Here's an updated link to the relevant Groovy feature: http://groovy-lang.org/operators.html#_spread_operator

@munificent
Copy link
Member

This is a pretty neat idea—Groovy has quite a few good ideas in it, but we don't feel it's a good fit for Dart. In general, my rule is to avoid operators or punctuation syntax unless you use it to refer to something that users have already learned. + for addition is fine since we learned that in grade school.

The problem is that if you use punctuation for something novel, users can't read it. If I've never used the skip method before, I might not know what it does, but I do at least know what the word "skip" means in English. But if that method used, I don't know -&, then I'd be stuck.

Passing a lambda to .map() is a little verbose in Dart, but I'd rather improve that by making small lambdas more terse in general than I would adding special syntax for map() and map() alone.

@munificent munificent added the closed-not-planned Closed as we don't intend to take action on the reported issue label Dec 19, 2016
@nic-hartley
Copy link

Splat/spread is useful for more than just maps. For example:

[
  *someArray,
  someIndividualThing,
  someOtherIndividualThing
]

...would give an array containing each element of someArray, followed by someIndividualThing and someOtherIndividualThing. Similar for function arguments. It's certainly a niche feature, and can be replicated easily enough with other things, but it makes array-building code a little cleaner.

@zoechi
Copy link
Contributor

zoechi commented Jan 3, 2019

@nic-hartley that's discussed/planned currently. Check the dart-lang/language repo

@munificent
Copy link
Member

Right, we are planning to implement this now. See: https://github.com/dart-lang/language/blob/master/accepted/future-releases/spread-collections/feature-specification.md

@nic-hartley
Copy link

nic-hartley commented Jan 3, 2019

Oh, neat! I didn't see the other repo. Cool!

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-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants