-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: disallow return
inside generator and comprehension expressions
#27034
Conversation
I guess we're not doing this, then? :/ |
We can still discuss it, I just decided to fix the bug (the discrepancy between the special lowering and normal lowering of comprehensions) first. |
We did talk about this on the triage call — folks there seemed to be generally in favor since it's not at all obvious what you're returning from. |
It is not actually unclear at all: you are returning from the innermost function body—that's always what |
To split a hair: sure, it may be a well-defined behavior, but I maintain it's not obvious — at least it wasn't to me. I fully understand why it works the way it currently does, but my initial thought pattern here went something like:
As Jeff said in the first post: "it's not lexically obvious that the generator expression is nested inside a function." If return from do-blocks was ever questionable (#1288), this is even more so. |
A do block is just a syntax for passing an anonymous function, comprehensions are not—if an anonymous function is used in the implementation of a comprehension, that implementation detail should be hidden. For do blocks, it's not an implementation detail, it's the definition of what the syntax means. In this case, whenever the |
To be clear, I'm not arguing for allowing this—I don't care—I'm just arguing that if we did allow this there is only one correct behavior. |
Oh, well since comprehension bodies are implemented as functions, that is in fact what it will return from currently. Making it do anything else is pretty difficult. This is partly exposed via generators; if you inspect the object Consider this example:
If the |
Sure, I can see that it's hard to implement and highly marginal, so just disallowing it seems good. |
Perhaps |
Ah, now I see how we were talking past each other — I was talking about how the currently-implemented behavior is not obvious. We're in agreement. |
This allows us to avoid defining whether a generator expression is wrapped in a function, and avoids possible confusion about where the
return
will actually return from (since it's not lexically obvious that the generator expression is nested inside a function). Alternatively, we could decide that a generator officially introduces a nested function, and fix this case in the special alternate comprehension lowering.ref #27023