Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

By that argument a recursive function shouldn't create a new scope every time it recurses, and a language that fails Knuth's 1964 benchmark of reasonable scoping (the "man or boy test") would be fine. The loop body is lexically a block and like any other block it should have its own scope every time it runs.


Except that the for loop does not create a new scope and is not a block:

https://docs.python.org/3/reference/executionmodel.html#stru...


Also, loop bodies already did have their own scope each iteration.

I wouldn't say either behavior is non-lexical. The only thing changing is which lexical scope these variables go into.


If the loop "variable" (and IMO thinking of it as a variable is halfway to making the mistake) is in a single scope whose lifetime is all passes through the loop body, that's literally non-lexical; there is no block in the program text that corresponds to that scope. Lexically there's the containing function and the loop body, there's no intermediate scope nestled between them.


> and IMO thinking of it as a variable is halfway to making the mistake

I used plural for a reason.

> there is no block in the program text that corresponds to that scope.

The scope starts at the for. There is a bunch of state that is tied to the loop, and if you rewrote it as a less magic kind of loop you'd need to explicitly mark a scope here.

What's non-lexical about it? You could replace "for" with "{ for" to see that a scope of "all passes through the loop body" does not require anything dynamic.

And surely whether a scope is implicit or explicit doesn't change whether a scope is lexical. In C I can write "if (1) int x=2;" and that x is scoped to an implicit block that ends at the semicolon.

Would you say an if with a declaration in it is non-lexical, because both the true block and the else block can access the variable? I would just say the if has a scope, and there are two scopes inside it, all lexical. And the same of a for loop having an outer and inner scope.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: