dart-lang / dart-lang/language
Object Spread operator
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Consider adding a spread operator to classes.
This could be useful for objects from `fast_immutable_collections` which dont implement `Map/List/Set`
```dart
class SomeClass {
String get foo => 'foo';
int get bar => 10;
List operator ...[] => [foo, bar];
Map operator ...{} => {'foo': foo, 'bar': bar};
// for record spreading, if added (not a parameter list - acts like a getter. maybe a different distinction system?)
({String foo, int bar}) operator ...() => (foo: foo, bar: bar);
}
// {'foo1': 'foo2', 'foo': 'foo', 'bar': 10, 'bar1': 420}
final map = {
'foo1': 'foo2',
...SomeClass(),
'bar1': 420,
};
// ['foo1', 'foo', 10 , 420]
final iterable = [
'foo1',
...SomeClass(),
420,
];
// (10, 'bar', foo: 'foo', bar: 10, foo1: 'bar2')
// of static type (int, String, {String foo, int bar, String foo1})
final record = (
10,
...SomeClass(),
'bar',
foo1: 'bar2',
);
// perhaps in the future
void fn({required String foo, required int bar}) {}
void invoke() => fn(...SomeClass());
```
Spread operators would reclusively call until a valid spreadable appears
classes like `Map`, `Iterable`, (and maybe `Record`) could have an `external operator ...[]|{}|()` which *actually* spreads the values
ex:
```dart
class IList /* doesnt implement list */ {
// whatever
List _internalList;
List operator ...[] => _internalList;
}
final iList = IList(['one', 'two']);
final List list = [...iList];
```
related #3448 #1293
Contributor guide
Research direction
Start with the proposal in this issue and compare the related issues #3448 and #1293. Examine the requested spreading behavior for classes, maps, iterables, and records, then determine the language-design decisions needed; done means the proposal has a resolved design direction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100