[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Non-lexical (lexical) scoping
- From: Sean Conner <sean@...>
- Date: Tue, 20 Mar 2018 02:37:17 -0400
It was thus said that the Great Egor Skriptunoff once stated:
> On Tue, Mar 20, 2018 at 1:38 AM, Soni "They/Them" L. wrote:
>
> > the ability to undefine variables.
> > That ability is itself lexically scoped
> >
> >
> Why the heck do you need this?
I have a few ideas:
1) I just thought of this and I think it's a good idea. Let me toss
this out there and see if it sticks! [1]
2) To get to upvalues that are masked by the same name:
local a = 1
function foo(x,y)
local a = 2
if x + y < 0 then
forget a -- get to the a that is equal to 1
return a
else
return x + y + a
end
end
Tis a silly example, but maybe gets the idea across. Why anyone
would *want* this I have no idea, since it assumes a particular
stacking of scopes and I'm not aware of anyone anymore who thinks
that dynamic scopes [2] is a good idea.
> Show real-life example, please.
I'm curious as well.
-spc
[1] I did this once, only it was for a feature in C that I used to do
under Assembly. Turned out to be a *very* bad idea---worse than
setjmp()/longjmp() (which it was somewhat, kind of, similar to).
[2] Example of dynanmic scope, assuming Lua support it:
x = 1
local function foo()
print(x)
end
local function bar()
local x = 2
foo()
end
foo()
1
bar()
2
foo() will use the first x it finds along the call chain, so when
bar() calls foo(), foo() will see the x in bar and print it.
This is a *bad* idea. It's one of the few bad ideas from Lisp. It
makes debugging in a large codebase more difficult.