lua-users home
lua-l archive

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




On Sat, Jan 18, 2020 at 2:44 AM Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:

It seems that there is a possibility to make next step in that direction:
to merge Lua tables and Lua functions in a single data structure.

You may have noticed that there is a similarity between how we
construct an array and how we define a function:
   arr = {11; 22; 33}
   function func() local x=42; x=x*x; return x; end


you could have this data structure right now, in pure Lua!
```
local insert = table.insert
local func = {}

insert(func, function() func.x = 42 end)
insert(func, function() func.x = func.x * func.x end)
insert(func, function() return func.x end)

setmetatable(func,
   { __call = function(tab)
         for i = 1, #tab - 1 do
              tab[i]()
        end
       return tab[#tab]()
end})

func()
```

I'm not sure what you plan to do with it, but, there it is.

cheers,
-Sam.