lua-users home
lua-l archive

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

Of course the recursive nature of such a model is obvious (I was aware of it when I suggested empty), but I stand by my argument: Since Lua arrays cannot be sparse, you must either (a) structure the data in a form that is not an array or (b) have a sentinel value that acts as a placeholder for "empty" elements. Since Lua has no language-defined placeholder, it is necessary to choose an existing Lua value to act in that capacity, be it false, an empty table, userdata, or the string "this-is-not-an-element".

Such a choice is domain-specific, which is fine in a closed application model, where you are free to create the necessary conventions (and hopefully enforce them). But when developing libraries for more general use, plucking a convention out of the air is asking for trouble. Everyone will choose a different one, and interoperability issues turn up. My feeling was that a sanctioned placeholder would help alleviate this situation. After all, why does SQL have NULL values? You could argue it doesn't need them for the same reason Lua doesn't need empty, but it would force each table schema to define it's own "placeholder" in a domain-specific manner, which I don't think the database community would like much.

And yes, of course you can store empty into an array (I believe I did answer this). As I stated, "empty" to my mind is simply a regular Lua value that is not equal to any other value except itself. So it can be used as a value, a function argument, a return value, a table key etc etc. Doesn't seem too complex a concept to me, but maybe I'm missing something obvious...


On Jun 28, 2013, at 2:24 PM, Roberto Ierusalimschy <> wrote:

>> My argument was:
>> (a) I see this as a useful generic language feature, that while lightweight is also light to implement and understand.
>> (b) It cleans up one of the side-cases of Lua, the odd behavior of Lua arrays when faced with nil values.
> Maybe I am missing something obvious, but Lua already has an "empty"
> value. It is called "false". Of course, it is not a perfect solution,
> because now your array cannot have "false" values, as they denote
> holes. So, ok, we create a new "empty" value to denote holes. But,,,
> wait! As Dirk asked repeteadly without an answer, what if we want to
> store an "empty" value into an array? After all, "empty" is a valid value
> in the language, is it not? Functions can return "empty" (e.g., rawget),
> and therefore I may want to store it in an array (e.g., all the results
> of a function, or the results of an array of functions).
> The solution seems obvious again: we create a new value, "undefined". But,
> wait!...
> -- Roberto