lua-users home
lua-l archive

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


On Oct 1, 2013, at 6:51 PM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:

>> I still feel the lack of an efficient way to determine if a table t is a sequence is a significant hole in the language/runtime.
> 
> Why would you need that? If you care that a table is a sequence, then either
> you have created the table and know it or you have been handed a table and
> can assume whoever gave you the table honors the contract with you to use
> only sequences. What am I missing?
> 

Seriously??? I'm stunned that you can seriously suggest "honors the contract with you to use only sequences". So if a 3rd party makes an error calling an API, it's ok to crash or generate random erroneous results? Where in Lua is this behavior exhibited? Is it ok for load() to skip over syntax errors and create an executable chunk that runs random, invalid, VM code? Is it ok for math.sin() to generate random values if I supply a boolean or (non numeric) string as input?

No, these APIs validate and rigorously check input over their expected domain, and generate errors accordingly (what else *is* syntax checking???). They protect themselves from erroneous input, as much as is practical. This is fundamental good design of APIs.

Scenario: I'm writing a 3rd party API that expects a sequence as an argument. For example:

sum = addSeq(t)
The addSeq() function adds all the values in the sequence in the table t, returning the total.

What should the API do if "t" is NOT a sequence:
(a) Return a random result that is incorrect (or crash)
(b) Flag an error

You seem to be suggesting (a), which I regard as flawed. I think a robust API should validate it's input as far as possible. Checking simple things like the types of arguments is trivial, so I can check that "t" is a table (as I should). But can I check that "t" is a sequence? No, I can't, or at least I can't trivially .. I can write a checker function, but any Lua code that checks to see if "t" is indeed a sequence is non-trivial and expensive .. and this is the hole: It's EXPENSIVE to check that a table is a sequence and therefore EXPENSIVE to create a robust API in this case.

The root of this problem is as I have said before: The Lua contract says "If a table is a sequence, the # operator returns the length of the sequence, if a table is not a sequence, the # operator generates an undefined result. There is no language provided facility to validate if a table is a sequence".

This translates to: "The # operator has undefined behavior if you cannot assert that a table is a sequence".

This in turn translates to: "You cannot use the # operator unless you originate or trust all tables with which you use it".

This finally means, "You cannot use the # operator with tables supplied by 3rd parties (such as callers of an API, since you cannot trust 3rd parties)".

Which leaves you scratching your head if you wish to write a robust, production quality, API that can accept 3rd party input and generate sane results if that input is expected to be a sequence.

--Tim