lua-users home
lua-l archive

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


Dirk and I had a serious go at worrying at the table library a few months back and here is what I came away from that experience with (aside from the bruises which have not yet completely healed!)

1. The current table library is not a table library, it is a list library. But there is a lot of stuff in the basic functions that ought to be moved to a table library (or retired).

2. 'ipairs' and its associated metamethod are redundant - just use the numeric form of 'for'. This realisation suggests a re-evaluation of the approach in my "Self-iterating Objects" powerpatch.

3. Multiple inserts (and more spectacularly, removes) are a clear opportunity for performance optimisation, but it is easy to do this in Lua. Having the library functions just encourages inefficient coding.

4. Everything except 'sort' can be done (almost) as efficiently (and to taste) in Lua. It would be useful to have 'sort' surfaced in the C API as well as a Lua library function.

5. Banging your head against a brick wall is a lot more fun than trying to sort out the steaming conceptual and practical mess that lies behind the # operator on table type! As us Irishmen are supposed to say "if I wanted to get there, I wouldn't start from here"!

My approach is to remove everything except 'sort' from the 'C' table library and use my own Lua implementation with an OO approach to wrapping table as Table, List and Set classes. But it still bothers me that the current metamethod design does not allow me to intercept assignments that would break the objects (changing an existing List value other than # to nil, assigning other than 'true' or 'nil' to an existing Set member.)

By implementing Classes as constructor functions with the metatable referenced in an upvalue, we can have a rather neat extension of the table constructor syntax:

tab = Table{first="Doris";last="Lessing"}
lst = List{"Doris Lessing";"Margaret Atwood";"Iain Banks"}
set = Set{["Literature"];["Science Fiction"]}

(The last requires my "Set syntax shortcut for table constructors" powerpatch which defaults the values to "true").

NB: I'm not trying to sell this concept for standard Lua (I realise OO is a hard sell in some quarters), just that it could be more fully supported by the underlying mechanisms.

> Date: Sun, 24 Nov 2013 10:21:47 +0200
> From: Dirk Laurie <dirk.laurie@gmail.com>
> Subject: Re: table library changes (was Re: table.new in 5.3?)
>
> 2013/11/24 Tom N Harris <telliamed@whoopdedo.org>:
>
> > If you're going to be toying with the table library, how about
> allowing
> > table.insert and table.remove to work with multiple values.
> >
> >     table.insert(list, value)
> >     table.insert(list, pos, value, ...)
> >     table.remove(list, firstpos [, lastpos])
>
> I support this, of course, but ... been there, done that, got T-shirt.
>


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com