dart-lang / language

Design of the Dart language
Other
2.66k stars 205 forks source link

Spread Collections #47

Closed munificent closed 5 years ago

munificent commented 6 years ago

Solution for #46.

Feature specification.

Most other languages have a "spread" or "splat" syntax that interpolates the elements of an existing collection into a new collection. In JS, it's a prefix .... In Ruby, Python, and a couple of others, it's a prefix *.

I propose we follow JS and use ....

This proposal is now accepted. Implementation work is being tracked in #164.

cbazza commented 5 years ago

// Just for reference, check out Swift's operator creation functionality... very powerful. https://medium.com/@vialyx/swift-course-advanced-operators-1c86f4ae7d67 https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html

Solido commented 5 years ago

Very secondary question but why ... instead of *. Quoting the documentation :

The expression following yield* must denote another (sub)sequence ...

In a sync* function, the subsequence must be an iterable ...

So in my mind * is binded to sequence and then when spreading it goes to ... that is the JS expression.

Is there any reason for having two symbols for the same concept of sequence ?

lrhn commented 5 years ago

The * is less conspicuous and easier to confuse with multiplication. It's not impossible to see things like:

var list = [
  something,
  someLongValue
  * example;
];

Here I forgot the , after someLongValue, and that turned the expression into a multiplication. The ... is not used anywhere else, and it stands out when looking at the code.

I can see that yield* uses * as sequencing, and we were actually toying with using yield and yield* in the syntax. It just wasn't worth it. Also, I might also be damaged by a history of C programming, where *list means something quite different.

So, in short, the ... for spread is used in multiple languages, and it isn't generally used for something not related to sequences. The * is used for spreading in multiple languages, but also with different meanings in both Dart and other languages, and it's easier to make mistakes with it. The ... syntax had about the same number of pros, and fewer cons, than *.

aartbik commented 5 years ago

@aartbik

mit-mit commented 5 years ago

Closing; this is launching in Dart 2.3