lua-users home
lua-l archive

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


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.

-- Martin