-
Notifications
You must be signed in to change notification settings - Fork 373
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
A statement pulled out of the condition of a while
loop is only evaluated once
#1362
Comments
How should a The problem with the second option is what happens with nested loops. If the The first option only puts the condition inside the body, so it will only break the inner loop, which is still weird, but at least it's consistent. We could probably disallow Maybe there's some other way to compile this that gives us sensible |
A third option. x = 3
_hy_anon_var_1 = True
while _hy_anon_var_1:
# Condition
print('hi')
_hy_anon_var_2 = x
if _hy_anon_var_2:
# Body
print(x)
x -= 1
else:
_hy_anon_var_1 = False This is like the first option, but we don't add our own [Edit: we don't need two anon vars] x = 3
_hy_anon_var_1 = True
while _hy_anon_var_1:
# Condition
print('hi')
_hy_anon_var_1 = x
if _hy_anon_var_1:
# Body
print(x)
x -= 1 |
Right, that's what I was thinking. I'm not too worried about this in any case because putting a loop-control statement in a loop condition is a weird thing to do in the first place. "Doctor, it hurts when I do this", etc. Here's another version I thought of that's similar to your version:
The |
Oh, jinx! |
Haha. I realized I didn't need two anon vars so I edited it to simplify. I put the original version back for posterity. |
Good point about using from builtins import bool as _hy_anon_var_2 # from __builtin__ in Python2
_hy_anon_var_1 = _hy_anon_var_2(x) But in this case, we could avoid it altogether like this: x = 3
_hy_anon_var_1 = True
while _hy_anon_var_1:
# Condition
print('hi')
_hy_anon_var_1 = x
if _hy_anon_var_1:
_hy_anon_var_1 = True
# Body
print(x)
x -= 1 |
True. You could also write |
Hi, first: great job here with Hy. I'm looking forward for version 1.0. About this issue, why not creating a local function?
Does that mess up with the AST too much? |
We don't create anonymous functions when we don't have to because each function creates a new scope, meaning that |
Thinking about #1342 made me guess that the following would do the wrong thing, and indeed, it does:
The
print('hi')
only happens once instead of every time thewhile
condition is checked, because it's pulled out to before thewhile
.The most straightforward fix is to make a
while
loop with a complex condition compile to something like:(In practice,
Result.rename
would remove the use of_hy_anon_var_1
and just writeif not x:
.)The problem with this method is that an
else
clause won't be executed when the loop terminates normally, becausebreak
skips theelse
. This doesn't actually matter right now because we haven't yet implementedelse
forwhile
. But a method that's robust to this future added feature is:Note that it will then be unwise to use
break
orcontinue
inside the condition, because the condition executes once outside thewhile
and subsequently inside it.The text was updated successfully, but these errors were encountered: