[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Non-lexical (lexical) scoping
- From: "Soni \"They/Them\" L." <fakedme@...>
- Date: Tue, 20 Mar 2018 08:57:23 -0300
On 2018-03-20 03:37 AM, Sean Conner wrote:
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.
It's most useful in languages with strong brorrowck, like Rust. For example:
let mut x = Some(1.0);
if let Some(ref mut v) = x {
if cond {
forget v; // without this, we would error "cannot use `x` because
it was mutably borrowed",
// but since we are ending the scope of the borrow, we
can return x
return x;
}
*v = 3.0;
}
// do some other stuff here
In Rust, there's a proposal for non-lexical lifetimes somewhere, altho
I'm not sure if it'd solve this particular case.
In Lua, the only use-case I can think of is for accessing shadowed
variables in a block scope:
function f(a, flag)
local a = a or {}
local b = true
if flag then
forget a -- temporarily forget the shadowed `a` so we can get to
the original `a`'s nil/false-ness
b = not not a -- tobool operator
end
-- use the shadowed `a` normally
end
-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.
Ew, no. I've been showing examples since this thread started.
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.