I'd be disappointed if I've just moved this debate online so we get 'n' irreconcilable views rather than just 2!
The question I'd like to answer with this post: Is anyone interested in using xtable?
a) Interested if it has default sequence protection that is easily switched off
b) Interested if it has sequence protection which is off by default, but easily switched on
c) Interested only if sequence protection is removed altogether
d) Not interested - the standard table library is fine
I do not think removing, or not supporting (we could simply raise an error in xtable if there is no __len metamethod and len is not supplied as a parameter) is really an option. I only raised that to clarify my argument that if we support it then we should support it as safely as possible.
I am far from being a newcomer, and I got into this after being affronted when I finally tracked a bug that mysteriously emptied a list altogether. It turned out the bug was a negative pos parameter to insert (anyone want to defend Lua not making that an error?). The negative pos was not right there as a constant - it was due to a complex chain of events in another part of the program. Looking into this deeper I found that insert would also allow nil as a value and did not check the upper boundary either. Either of these could make the list mysteriously shorter or cause it to be emptied.
I do not see this as a problem with edge detection - though the non-determinism of that makes it worse. Rather is is inherent in the definition: edge detection implicitly defines a list as being terminated by a nil value, rather like a string is in C. Even if Lua found the size with a deterministic linear search from 1, nil is not an ordinary value which one can legitimately insert - it is special by definition. If you want nil to be an ordinary value you have to redefine # using a metamethod. Therefore, insert should not allow nil as a value if it is relying on the nil-is-terminator definition - QED!
I do not think the compatibility argument is strong - we break compatibility with the implementation (as opposed to with the manual) in a number of ways in xtable, some of them much worse than this. Dirk struggles to provide a reasonable use case for inserting nils in a default list (mostly involving doing some immediate fix-up afterward). If someone has done this in the real world, they will get a nicely localised and explicit error message, check the manual and find they need to replace:
insert(tab,pos,nil) --> insert(#tab,tab,pos,nil)
Hardly the end of the world! 99% of the time the error message will be flagging a real and highly obscure bug they've been trying to find for weeks.
Neither do I find the "warning to stdout" a good solution - many Lua implementations do not show, or make it easy to see such output.
- John