-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support continue
inside collection for
expressions
#4139
Comments
This is possible, but it isn't very useful imo. You can just as easily do For clarification, i'm not saying // for as expression
// for expression returns iterable, yield passes the value to iterable
final list = [
firstElement,
... for(final item in [1,2,3]){
if(item < 2) continue;
doSomething();
doSomethingElse();
yield item + 3;
},
lastElement,
];
// for -> Iterable<int> -> List<int> -> retList
var retList = for(final item in [1,2,3]){
if(item < 2) continue;
doSomething();
doSomethingElse();
yield item + 3;
}.toList(); But then again, both list comprehention and |
By that standard, we never need I agree that multiple statements in |
For this to happen, we would have to change dramatically how collection-if work. Currently, they expect an expression. We would have to have an expression in the form As for my personal opinion (because this issue is based on subjective perceptions), for (final item in list) {
if (condition) {
doSomething();
}
} Is better than: for (final item in list) {
if (!condition) continue;
doSomething();
} Also, I can't see how final list = [
for (final item in [1,2,3])
if (item < 2) continue
item,
] is better than final list = [
for (final item in [1,2,3])
if (item >= 2) item,
] |
The idiomatic way to do this in Dart is to use collection-if/collection-for instead of collection methods, so it just makes sense for the language to be more expressive in this regard. |
I understand the reasoning, but i don't really think that expanding what collection-for can do is the way to go. As i said, i think that Still, since collection-for is already part of Dart, i guess it is already decided. |
That's what I'm asking, yes. For a simple example, the difference may not be big. But you can technically chain if/for multiple times For example, we could do: if (!a) continue
if (!b) continue
for (...)
<expr> as opposed to: if (a)
if (b)
for (...
<expr> Widgets are already quite nested. |
I agree that there's value to a short |
Rather than adding more and more syntax, dart could optimize Iterable methods (where, map and friends) - e.g by inlining them whenever possible - so you would never need anything beyond [...[1, 2, 3]
.where(# < 2) // no extra indentation
.map(# + 10) // no extra indentation
.etc...
] (You can do it today, albeit with a huge hit in performance). |
@tatumizer Dart added collection expressions for a reason. Using iterables with widgets both has horrible readability and is a lot more limited. |
As pointed out, the way elements work, they are generally in tail position, so you don't need a The proposal is to introduce a new syntactic form if (expression) continue element which is really a "shorthand" syntax for if (expression) continue
element Doesn't seem like it's worth its own complexity. |
An idea is to have |
This would be basically an By having an unless-like condition, by itself, we would still have indented nesting: if! (a)
if! (b)
for (...)
<expr> Instead of the proposed if (!a) continue
if (!b) continue
for (...)
<expr> |
Hello!
Currently,
[for (...) item]
doesn't support thecontinue
keyword.Although we can express everything without it, this can lead to a lot of nesting.
The
continue
keyword is a very useful tool to unwrap some amount of nesting. For example:It'd be cool to support
continue
inside collections too:The text was updated successfully, but these errors were encountered: