Hacker News new | ask | show | jobs
by munificent 3208 days ago
> unless you'd like to be able to reference variables declared below you in the global scope.

It's this part. Unlike ML which uses Queinnec calls a "hyperstatic" top level environment, Lox follows Scheme where the top level environment is dynamic. This gives you a nice way to support mutual recursion at the top level.

> Or by making a single pass over the global space

That works when running from a file, but not in a REPL session. Lox also supports REPL sessions like:

    > fun foo() { return bar; }
    > foo();
    Undefined variable 'bar'.
    [line 1] in foo()
    [line 1] in script
    > var bar = "ok";
    > print foo();
    ok
> And that can be resolved by passing a dynamic environment in addition to the lexical one.

True, that would avoid the need for a single polymorphic environment type, but it's again still more code complexity.

> is a fairly common pattern in automatically curried languages, or languages with functional-style loops (see [0] for such a usage of shadowing).

Interesting, I wasn't aware of that. I can reword it by saying something more like "shadowing is usually in error in imperative languages".

Thanks!