lua-users home
lua-l archive

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



On Oct 2, 2013, at 1:53 PM, Sean Conner <sean@conman.org> wrote:

 Now, I'm harping on the sum example because that's the example you
presented.  It could very well be the case that the sequence { 1 , 2 , nil ,
4 , 5 } is a valid case, but Lua doesn't handle that, it's stated as such,
so there isn't much you can do there anyway.

 While I don't doubt you need to check for sequences, what exactly are you
trying to do with the sequences?  I only ask because for anything you are
trying to do, there exists a pathological case (just a reminder, the only
keys that aren't valid are nil and nan).

The sum was purely an example to illustrate the difficulty in creating a robust API. The issue here is the behavior of # when arrays have holes are can thus be viewed either as a "array with holes" or a "array with other integer keys after the end of the array". # gives odd results in these cases, which means (to my mind) its risky to use # when an array is not well-formed.

And thus I find it desirable to check to see if an array is well-formed before using # .. but there is no way to do this in Lua that is not O(n).

If this were all my code base, then, as others have suggested, the solution is "make sure you don't create ill-formed arrays", but it's NOT all my code base and I need to be defensive when writing APIs to be consumed by others.

--Tim