Hacker News new | ask | show | jobs
by muflax 3781 days ago
What languages without lexical scope are you thinking of?

Aside from literally only (pre-Common and Emacs) Lisp, I can't think of any language without lexical scope (outside some constructs etc).

[edit:] Or are you thinking of "only a single global namespace"? Even that exists only in otherwise highly restricted scripting languages, I think? ("Everything global" is technically lexical, but I see why you wouldn't want to call it that.)

1 comments

Tcl, PHP, any language without closures.
How does PHP have dynamic scope? It's been ages since I touched it, but if I remember right...

Global is just a flat namespace. Functions have their own, separate flat namespace each. I tried the following in a REPL:

function two() { echo $a; }

function one() { $a = 1; two(); }

one();

as the standard dynamic scope example, and I get an "undefined variable" error. Did that use to be dynamic? (I wouldn't be surprised, though, especially given PHP's implementation history...)

There is an explicit "static" modifier that looks dynamic, but I couldn't get it to run that way. It seems to just retain state in recursive calls of the same function, or something?

[edit:] (And PHP doesn't predate Javascript, but regardless, "no first-class functions" isn't "no lexical scope".)

Yeah, I shouldn't have used the term "dynamic scope" there. PHP has dynamic environments that can be mutated at runtime, which languages like JS don't (well, outside of direct eval).