4 ms·
Yes, the scoping in your example is lexical. That's why ugly returns k regardless of where you call it from; we can reason lexically to show that. The i in ug
by kragen 13y ago
Yes, the scoping in your example is lexical. That's why ugly returns k regardless of where you call it from; we can reason lexically to show that.
The i in ugly is lexically resolved to refer to a particular declaration, the one on the line below it. Scheme's lexical scoping rules are not the only possible lexical scoping rules. A lexical scoping system does not cease to be lexical simply because it is not the same as Scheme's lexical scoping. For it to be non-lexical, the scoping would have to depend on something other than the lexical structure of the program. For example, you could just as well have a dynamically-scoped JS in which ugly's return value would depend on its caller's binding for i, and in which calling a function with a free i from within example would make example's binding for i visible to that other function, but that isn't the way JS works.
It's quite reasonable to argue about whether that scoping is sane, and indeed with "let", the ES folks are giving us a saner binding construct; but it is certainly the case that the relationship between the declaration and the use of a variable is established by the lexical structure of the program text, not the dynamic structure of the executing program.
- untothebreach 13y agoThe "WAT" for me is that the `var i` is hoisted _out_ of the `for`, which IMHO, breaks the principle of least astonishment. I would expect the `i` to be lexically scoped to the body of the `for` block, instead of being hoisted out into the parent scope of the `for` block.
- iopq 13y agoI speculate that this is because Javascript takes stuff from Self where loops are functions. So probably Javascript was meant to write something like: while(function(k) { ++i; return i < k; }); where the while function would call the closure until it got false at which point the while function itself would return so having var declarations be hoisted was not a problem in this syntax because everything (including if and while) would have its own scope due to being a function
- kragen 13y agoYou could have designed a language like that, but Brendan didn't.
- gsg 13y agoI would argue that Javascript is not lexically scoped because the scope of a variable definition does not depend on the lexical structure of a program (with the sole exception of function boundaries). This is true even to the point of multiple definitions having no effect on the scope of a variable. I think that what you are arguing is that Javascript is statically scoped.
- kragen 13y ago"Statically scoped" is the same thing as "lexically scoped".
- munificent 13y agoIn the post, I specifically said Scheme had lexical block scoping. So, even if JavaScript had full lexical scoping (which it does not, thanks to with() and the global object), it still wouldn't have block scope (thanks to hoisting, though addressed in ES6), which I think is a defining feature of Scheme's approach to scope.
- kragen 13y agoYou're right about with(), which I'd forgotten about — JS with with() is not lexically scoped! Access to the global object is still statically decidable. It just might raise an exception. This is not really different from Scheme. I agree that block scope is a defining feature of Scheme's (and ALGOL's, and C's) approach to scope. However, Scheme's block scopes are all functions, at least if you accept the macro-definition of let that's been there since R5RS, rather than treating let as a separate primitive construct.