lua-users home
lua-l archive

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


Let inroduce new reserved word "pure_function" into lua.
It should work similar to usual function except
all variables used inside are implicitly local
no access to any upvalues or global namespace _G
So it could interact with arguments it was passed.

This allows to isolate parts of code from whole program.

In lua 5.3.4 there is no way to do it.

Here is example of isolation:

a=1
local b=1

function sandbox(_ENV)
  a=2
  b=2
end

sandbox{}

print("a=",a) -- will print 1
print("b=",b) -- will print 2

Changing environmet could not solve the problem, because of upvalues.
The behaviour depends on current scope. And the scope can be very complex.
This program keep global variable a, but modify b. Variable b is upvalue for sandbox function. If I define a local and b global result will change.

It would be perfect to have ability to isolate part of code from the rest.
It could be used for debugging, plugins and modules in lua.
It could look like:

pure_function sandbox(...)
  a=2
  b=2
end

So the behaviour will be same despite outside code. Even is a and b was declarate somethere upper.