lua-users home
lua-l archive

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



On 03/05/2014 18:04, Coroutines wrote:
The @function() for compile-time constant evaluation with constants for arguments is an too risky/general -- I can't predict all the pitfuls -- it was a nice daydream :-)
How about @function(...) which calls function(...) at compile-time and function(...) has to return a string that gets compiled as code?
Something like...

-----------------------------------------------------------
function localize(name)
    return "local "..name.." = "..name
end

@localize'print'
@localize'tostring'
--(etc)
-----------------------------------------------------------

and then it compiles to the same as

-----------------------------------------------------------
function localize(name)
    return "local "..name.." = "..name
end

local print = print
local tostring = tostring
-----------------------------------------------------------

Or something like...

-----------------------------------------------------------
local function turntable(...)
    local s = "{ "
    for x,y in pairs(...) do
        s = s .. y .. " = ".. y .. ", "
    end
    return s .. "}"
end

local env = @turntable("print","tostring","type","tonumber","pcall","xpcall")
-----------------------------------------------------------

Which would compile to...

-----------------------------------------------------------
local function turntable(...)
    local s = "{ "
    for x,y in pairs(...) do
        s = s .. y .. " = ".. y .. ", "
    end
    return s .. "}"
end

local env = { print = print, tostring = tostring, type = type, tonumber = tonumber, pcall = pcall, xpcall = xpcall, }
-----------------------------------------------------------