Sdk: Add "Spread operator"

Created on 29 Nov 2012  路  10Comments  路  Source: dart-lang/sdk

_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

P2 area-language closed-not-planned type-enhancement

Most helpful comment

All 10 comments

_Removed Type-Defect label._
_Added Type-Enhancement, Area-Language, Triaged labels._

_Set owner to @gbracha._
_Added Accepted label._

_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!

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

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

This is a pretty neat idea鈥擥roovy 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.

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmesserly picture jmesserly  路  3Comments

brooth picture brooth  路  3Comments

DartBot picture DartBot  路  3Comments

Hixie picture Hixie  路  3Comments

DartBot picture DartBot  路  3Comments