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

Comments

@DartBot
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Nov 29, 2012

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

@gbracha

This comment has been minimized.

Copy link
Contributor

commented Aug 25, 2014

Set owner to @gbracha.
Added Accepted label.

@DartBot

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Author

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;
  }

@munificent

This comment has been minimized.

Copy link
Member

commented Dec 17, 2016

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

@munificent

This comment has been minimized.

Copy link
Member

commented Dec 19, 2016

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.

@nic-hartley

This comment has been minimized.

Copy link

commented Jan 3, 2019

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

This comment has been minimized.

Copy link
Contributor

commented Jan 3, 2019

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

@munificent

This comment has been minimized.

Copy link
Member

commented Jan 3, 2019

@nic-hartley

This comment has been minimized.

Copy link

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
Projects
None yet
7 participants
You can’t perform that action at this time.