lua-users home
lua-l archive

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


> This seems like a contrived example

It is a contrived example.  It is technically possible to achieve the
same scope limitation (without the compile time error reporting) just
using the IIFE idea from Javascript and some discipline in code
design.  Consider:

```lua
local UV1, UV2
local LOCAL1, LOCAL2

local function funcA (arg1, arg2) <UV1, UV2>
  return arg1, arg2, UV2, UV1
end

local function funcB (arg1) <funcB>
  return arg1 > 0 and funcB(arg1-1)
end

print(funcA(LOCAL1, LOCAL2))
```

is equivalent to:

```lua
(function (funcA, funcB)
  local UV1, UV2
  local LOCAL1, LOCAL2

  funcA = funcA(nil, UV1, UV2)

  funcB = funcB(nil)

  print(funcA(LOCAL1, LOCAL2)
end)(
  function (_ENV, UV1, UV2) return function (arg1, arg2) local _ = UV1, UV2
    return arg1, arg2, UV2, UV1
  end end,
  function (_ENV) return function funcB (arg1)
    return arg1 > 0 and funcB(arg1-1)
  end end
)
```

The point for me is to know with certainty which upvalues a closure
can or cannot access.

I would actually be interested in trying to see if I can use the
`ltokenp` library to actually create this kind of scope limitation
without the parser change, although for my specific case I prefer
having the compile time error (although maybe `ltokenp` could do that
too?).

> It sounds like you are trying to solve several issues

It was more that I wanted to solve the issue of controlling upvalues,
and then once I decided I didn't like so much the wasted code in the
`local _ = UV1,UV2` solution I started trying to work out exactly what
the costs/benefits of controlling upvalues would be.  I actually
initially started with just trying to force the `_ENV` upvalue to
appear first by using the label `::_ENV::` as the first statement of a
function as a kind of pragma, but then I figured once I was starting
to change the parser at all I should try and find the most
comprehensive solution.

I think what I have that lets you simply declare upvalues as you would
function arguments is the simplest and most powerful solution.

Regards,

Duane.