lua-users home
lua-l archive

Re: table.remove bug?

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


Mark wrote:
> t = { "a", "b", "c" }
> table.remove( t, -1 )
> table.foreach( t, print )
> 
> --->
> 
> 1 b
> 2 c
> 0 a
> 
> Doing a remove of -1 again shifts it down further.
A bug? It depends on the point of view... It may be a feature...
First, the bug is in the call. The manual says nothing about negative
positions, so the result of this call is 
undefined.
Four possibilities:
- This is a feature, the behavior is on purpose. Perhaps it should be
documented...
- This is a bug, the function should raise an error on invalid input values.
- It is normal undefined behavior: as the authors often say, "garbage in,
garbage out", the C philosophy is 
not to do checks on input data (or minimal ones), it is up to the caller to
verify data it provides is correct. 
This allows faster routines. I believe that a scripting language should be
stricter, but why not.
- The behavior can be improved to accept negative positions, the actual
position being then "n + pos", ie. 
index from the end of the table.
Regards.
-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
+++ GMX - die erste Adresse für Mail, Message, More +++
Neu: Preissenkung für MMS und FreeMMS! http://www.gmx.net

AltStyle によって変換されたページ (->オリジナル) /