-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Provide Range function/class #42652
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
Comments
The That is, is Maybe we should have improved list interpretations: It could work for any type [for (var tmp = a; tmp != b; tmp += 1) tmp] and [for (var tmp = a, wasEnd = false; !wasEnd; tmp = (wasEnd = a == b) ? tmp : tmp + 1;) tmp] (or something more direct internally in the compiler) There is the obvious issue with ambiguous syntax for |
Another option is the "slice" syntax (e.g., dart-lang/language#1066) of This is currently unused syntax for lists because So, maybe we can use (We could also add slice operators to the language: |
I agree that it's unclear whether the argument is included or not. And yes, being able to create Either way, I agree that the current language doesn't allow for a terse creation of ranges, so (for now) this is more of a language issue and not an SDK issue, so I guess this one can be closed. |
Hang on. I did talk about syntax, but before closing this for real, I want to also discuss this argument:
Regarding clear syntax: Ranges being explicit or implicit is more a matter of convention than being able to see it in the function name – that's why the Effective Dart guide has a section about using inclusive start and exclusive end parameters to accept a range. The same argument of users not being able to see if Regarding confusion: Of course, Kotlin developers will feel right at home. Developers writing Note that while I do agree with the part of my previous conclusion that infix functions or slice operators would make the syntax terser, I still believe that built-in Edit: It came to my attention that Kotlin doesn't actually have a |
If the right-open/right-closed issue confuses people you can make it explicit, kind of like Rust has:
This syntax is also nicer than |
@Timmmm Yes, I totally agree with you. That's a language feature though, not an SDK feature, so it would require a lot more changes (even changes in the compiler stack) and take much longer to implement. Adding a first-level |
@Timmmm I especially like this syntax. It's distinct, unambiguous, fits with Dart's general style, and will not conflict with any other operators. I don't know how much extra work it would require though; if what @MarcelGarus says is true, it probably isn't worth it. |
More discussion over at dart-lang/core#624 |
In Dart code, I see code with the following structure very often:
This is a very imperative way of programming (first, setting
i
to0
, then checking ifi < 10
and after each iteration increasingi
by one).Most people try to code in a higher-level declarative way and I thought it's weird that the Dart core SDK doesn't have something like a
Range
that could be created with ato
extension method onint
so that you could simply doSure, there are third-party packages that offer this functionality, but this is such a fundamental, basic functionality that I believe it's common enough to be in the standard library. I'm looking forward seeing this being integrated into the platform.
The text was updated successfully, but these errors were encountered: