3 ms·
Dynamic scope/binding is super useful, but I do not agree tcl does it or does it well, because it's not just shadowing the closure environment (ha), or changing
by geocar 1y ago
Dynamic scope/binding is super useful, but I do not agree tcl does it or does it well, because it's not just shadowing the closure environment (ha), or changing globals in a dynamic-wind.
Perl's the only non-lisp I'm aware of that does dynamic binding well-enough to get the idea, but too many things just don't work:
local *int = sub { return 69 }; print int("42");
is a particularly annoying one: You just can't do it. Other names might have better chances, e.g.
package foo; sub int { 42 };
package main; sub a{local *foo::int = sub { return 69 };&b(@_);} sub b{goto \&foo::int};
print b("x"), a("x"), b("x"), a("x");
but this is a little unsatisfying.
- eru 1y ago> Perl's the only non-lisp I'm aware of that does dynamic binding well-enough to get the idea, [...] Well, Haskell does dynamic binding in the form of the Reader Monad. It's well received there.
- geocar 1y agoErm no. The point is I want to have the ability to make any variable dynamic, not just work with a global variable that is really a getter.
- eru 1y agoThe reader monad doesn't need to be global. But yeah, by that standard, Haskell implements both dynamic scoping and mutable state via getters and setters. (Though, of course, you can go all out on these via lenses.)