lua-users home
lua-l archive

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


Dirk Laurie <dirk.laurie <at> gmail.com> writes:
> | The trouble with folk is not the things they don't know, but the
> | things they know that ain't so. — Josh Billings

"Stupid is as stupid does" - Forest[1]

> I'd say the mere use of the operator means that the
> table creator "knows" (in the Billings sense) that the table is either
> a sequence or properly metamethoded.

Therein is your fallacy. The creator self knowledge was never in question, it is 
the consumer of the table that may not know much about the table (assuming lack 
of contract).  As such, it is important to know if the length operator can be 
trusted or not.  As it stands, it can not be trusted and so is useless without 
an external contract in place between the creator and consumer of the table.  
Following my recommendation that the length operator always return nil unless 
__len metamethod exists, the creator of the table who "knows" what the table is 
has the ability to define such a metamethod, and for a sequence, it could wrap 
the proposed table.len() function.  So continuing this fantasy, it would be the 
case that if the length operator returns a non nil value, it purportedly can be 
"trusted".

> An inferred principle of Lua design is: protect idiots against themselves,
> but not at the cost of performance.

For the most part I find Lua to be a very simple and elegant, not that my 
finding is authoritative.  But, you don't get that by "protect idiots against 
themselves", rather you get bloated <censored language name to avoid flame war>.

> Testing whether a table is a sequence is very expensive.

Exactly true, which is why the current implementation is such a problem.

> Forcing innocent non-idiots into extra work is also out.

You are assuming a single use case (the owner is the consumer), in fact by not 
requiring explicit definition for the __len meta method, one is left with no 
choice but to ignore the availability of the length operator and completely 
iterate over the entire table.  Which is far more work at a high cost.

chris

[^1] A more contextual variant of "Handsome is as handsome does" - Esa