lua-users home
lua-l archive

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


On Jun 1, 2010, at 12:27 PM, Oliver Schmidt wrote:

> 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" }

This seems problematic from the standpoint that it won't let you append a table-based object into the target.

But turning to the core request, what you could do instead is something like:

	Stream( "a", "b", "c" ):append( "d", "e" ):append( "f" ):appendElements( t2 ):append( "z" ):extractTable()

This is, of course, much more wordy. So, how about:

	Stream( "a", "b", "c" )( "d", "e" )( "f" ):elements( t2 )( "z" ):extractTable()

The stream object here implements the __call metamethod as a shorthand for append.

Mark