[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: new "empty" value/type in Lua?
- From: Tim Hill <drtimhill@...>
- Date: Thu, 27 Jun 2013 17:57:33 -0700
I'm going to get shot for suggesting this… (hides under bed)
I've been writing a lot of Lua code recently, some of it quite generic in nature. One thing I frequently come up against is the need to handle data polymorphically in tables. And an effect of this is to hit against the overloaded use of "nil" in Lua.
It seems to me that "nil" is overloaded; it means both a noun: "no value" and a verb: "delete this table entry". Now, most of the time, the delete is hidden since reading a non-existent table entry returns nil. So having an explicitly stored nil versus having no value stored at all is equivalent (all this I love, btw … 100 points to Lua).
But it all rather breaks down with arrays (sequences). I cannot have sparse arrays, which in Lua terms means I cannot have "nil" in an array element. So, when writing polymorphic code, how do I handle cases (which, trust me, i DO have to handle, with "no value" in an array element? There are, of course, a whole bunch of work-arounds: Use a sentinel value; use an empty table (assuming empty tables aren't value data, and ignoring that each one is unique); use a magic userdata (assuming I have access to C code to make one); etc etc.
But, let's face it; they ARE all workarounds. I think there IS a case to be made for being able to store a value in a Lua array that is uniquely NOT any other Lua value. Essentially, I think that value is "empty"; a valid Lua value that is simply not the same as any other Lua value (including nil).
Basically, empty would act like nil in most regards, except:
-- Storing empty in a table element actually stores the value, rather than deletes it.
-- When used in a boolean context, the empty value evaluates to true (unlike nil).
-- Has a type() of "empty"
Other arguments in favor of this:
-- Judging by the mail list chatter, a lot of Lua beginners struggle with the problem of nil in sequences.
-- Better impedance matching to SQL NULL values; since empty can bridge to a SQL NULL when a Lua array is used to hold a SQL row.