-
Notifications
You must be signed in to change notification settings - Fork 213
Add the 'nonlocal' keyword to allow non-local returns #1776
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
Well in this case, there's obviously An alternative that's commonly used is to return a value with some meaning, and then the enclosing scope looks for and interprets that value. For example, bool any(bool test(E element)) {
for (E element in this) {
if (test(element)) return true;
}
return false;
} In fact, that solves your example just as well as bool contains<E>(Iterable<E> hasystack, E needle) =>
iterable.any((E item) => item == needle); There can also be an advantage to letting the enclosing scope have full control over the lambda, instead of giving the lambda free reign to jump out-of-scope: bool findInFile(File file, bool Function(String) predicate) {
// in order to close the file, the stream must be fully read or cancelled
bool found = false;
await for (final List<int> chunk in file.openRead()) {
final String decoded = utf8.decode(chunk);
if (predicate(decoded)) found = true;
}
return found;
} A |
I think non-local returns are important in a language like Smalltalk that doesn't have a lot of built in control flow syntax and does have an extremely lightweight syntax for blocks. You basically need them to implement useful control flow. But Dart has |
That makes sense. |
I'm going to close this because I don't think the language team is likely to do nonlocal returns any time soon. |
Ok |
I was recently reading this blog post by @munificent. I came across the
Beatiful example 2: Finding an item
section and Bob pointed out how Dart doesn't have non-local returns. After seeing a little bit of other languages where lambdas can return from the scope outside of them, maybe Dart should have a feature like that.The nonlocal keyword would mean that any
return
(or evenbreak
,continue
, oryield
?) statements perform that action in the scope from which the lambda was called, and not just inside the lambda itself.That's about it, so tell me if this is a good or terrible idea, I suppose.
The text was updated successfully, but these errors were encountered: