lua-users home
lua-l archive

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


Hi,

On 09/05/10 17:36, Klaus Ripke wrote:
Could x[] be syntactic sugar for x[#x+1] ?
It would make appending to lists so much nicer.

I also think that a nice syntactic form for appending to lists would increase Lua's usability as configuration language.

Consider the use case, that the user has to configure some lists of names (e.g. filenames) for some kind of processing. Some names may belong to different lists. So it would be nice, if the user may define his own lists and build configuration lists together from his user defined lists. So the user can group his names and mention each name only once.

IMHO a nice way for this could be a stream operator "<<" (this could also be useful for other use cases).

E.g.

    t1 = { "a", "b", "c"}
    t2 = { "x", "y" }

    t1 << { "d", "e" } << "f" << t2 << "z"

Would result in t1 pointing to the table { "a", "b", "c", "d", "e", "f", "x", "y", "z" }

The stream operator should be overwritable in the metatable, with default implementation:

    function __stream(a, b)
        if type(a) ~= "table" then error("...") end
        if type(b) == "table" then
            for i = 1, #b do
                a[#a + 1] = b[i]
            end
        else
            a[#a + 1] = b
	end
        return a
    end

What do you think?

Best regards,
Oliver