lua-users home
lua-l archive

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


In Lua the f{...}syntax (syntactic sugar for f({...})) is useful for data description:

    SomeType {
        1,
        2,
        3,
    }

What if we want to allow the user to name this list data?  One way is like this:

    SomeType {
        name = "abc";
        1,
        2,
        3,
    }

But that requires the user to remember to use ';' and do extra typing, and exposes internals of
the implementation.  A syntax like this may be preferable:

    SomeType "abc" {
        1,
        2,
        3,
    }

This can be done in Lua with a wrapping function that takes the name and generates a real
constructor.   Here is an example:

    function SomeType(name)
        return function(t)
            t.name = %name
            return t
        end
    end


-John