lua-users home
lua-l archive

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


On Thu, Jan 9, 2014 at 12:20 PM, Fabien <fleutot+lua@gmail.com> wrote:
match data with
| { name, x, y } -> return { sprite = '/img/'..name, x=x, y=y }
| { name} ->
    local filename = '/img/'..name
    local x, y = getDimensionsFromExternalTool(filename)
    return { sprite = filename, x=x, y=y }
| _ -> error "invalid pattern"
end

To be clear, this is turned at compile-time into some code equivalent to:

if type(data)=='table' and #data="" then 
    local name, x, y = unpack(data);
    return { sprite = '/img/'..name, x=x, y=y }
elseif type(data)=='table' and #data="" then
    local name = unpack(data);
    [...]
else error "invalid pattern" end
 
(Although debug info still points to the proper line numbers in the original, readable sources). All you have to do to use the extension, supposing that you have the metalua-compiler rock installed, is to add at the beginning of my_lib.mlua the magic line "-{ extension('match', ...) }", put the file in your path, and invoke it with:

lua -l luarocks.loader -l metalua.loader -l my_lib code_using_my_lib.lua

So even if your lib is written in Metalua, it's still usable from plain Lua.
I believe you can install the rock from the github sources with:

luarocks make metalua-compiler-0.7.2-1.rockspec --local