You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider adding a spread operator to classes.
This could be useful for objects from fast_immutable_collections which dont implement Map/List/Set
classSomeClass {
Stringget foo =>'foo';
intget bar =>10;
List<dynamic> operator ...[] => [foo, bar];
Map<String, dynamic> 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 futurevoidfn({requiredString foo, requiredint bar}) {}
voidinvoke() =>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:
classIList<T> /* doesnt implement list */ {
// whateverList<T> _internalList;
List<T> operator ...[] => _internalList;
}
final iList =IList(['one', 'two']);
finalList<String> list = [...iList];
The record version probably needs record subtyping to allow sub classes to override it to add more parameters...
That one can probably be held off on for now
Seems wasteful to create a list, just to immediately throw it away again after spreading.
Instead of introducing a specialized operator that can only be invoked inside specific contexts, how about just making it a getter or function:
List<dynamic> get asList=> [foo, bar];
Map<String, dynamic> get asMap => {'foo': foo, 'bar': bar};
and spread it as:
final map = {
'foo1':'foo2',
...SomeClass().asMap,
'bar1':420,
};
(Coding style says to make such asX members into functions instead of getters, though.)
Or to put it shortly: don't be an Iterable, have an Iterable. Favor composition over inheritance.
(Or admit to being an Iterable, rather than having an operator that implicitly converts the object to an Iterable, and which only works for spreading. Why not also for/in iteration?)
Consider adding a spread operator to classes.
This could be useful for objects from
fast_immutable_collections
which dont implementMap/List/Set
Spread operators would reclusively call until a valid spreadable appears
classes like
Map
,Iterable
, (and maybeRecord
) could have anexternal operator ...[]|{}|()
which actually spreads the valuesex:
related #3448 #1293
The text was updated successfully, but these errors were encountered: