language
language copied to clipboard
Add `yield` to collection for
I constantly feel that collection for
is almost there, but not there. It allows some cool syntax for simple cases, but constantly I need to add just a few more steps, like getting data from an async call, then deciding if I should add it to the list or not.
With the current syntax, we can use {}
, so we are restricted to simple 1 line cases.
This made me think that we might (I am not aware of the complexity) combine generators and collection if
(at least in the syntax, merging their functionality might make the code more complex or with worse performance), to allow us something like:
Future<List<String>> getStrings() async {
return [
for (final myInt in myInts) {
final myString = await getMyData()
if (myString != null) {
yield myString; // This will signal to the collection for that we are producing a new item.
}
}
];
}
With the yield
keyword we could leverage collection if
to spread across several lines.
I apologize in advance if I am missing something, I bet I am, as language features are always tricky.