lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2018-03-20 07:15 PM, dyngeccetor8 wrote:
On 03/20/2018 02:57 PM, Soni "They/Them" L. wrote:
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
I think better way is to allow indirect access to lexical scope
variables. Like "_G" but populated with local variables. (And
some awkward name to access outer level, say "__parent".)

So you code example becomes

   function f(a, flag)
     local a = a or {}
     local b = true
     if flag then
       b = not not _ENV.__parent.a -- tobool operator
     end
     -- use the shadowed `a` normally
   end

I believe this may be implemented but will introduce much more
problems than solve.

Bleh.

Idk, I've touched the Lua internals before. I'd say "forget" is much easier on the register (de)allocator stuff.


-- Martin


--
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.