[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: The Undefined Country (was Re: Quest: real world "Lua array with holes" usage)
- From: Andrew Starks <andrew@...>
- Date: Sun, 24 Jul 2016 22:58:27 +0000
On 2016-07-24 22:26, Sean Conner wrote:
> Proposal: undef. It works just like nil does now, but "nil" (the new nil
> behavior) is that it *is* allowed in sequences.
Why introduce a global concept for something that's just a problem in
some very special cases?
Nil works fine everywhere, except when you expect '{ ... }' to be a
sequence (or you use 'table.pack' and then get that 'n' field that some
people seem not to like...)
I present:
function table.packx( nilsubst, ... ) -- can't think of a good name
local t = table.pack( ... )
for i = 1, t.n do
if t[i] == nil then t[i] = nilsubst end
end
t.n = nil
return t
end
-- (Above function hereby put into the public domain, yadda yadda.)
Ta-daa! You get a sequence, no 'n' field, and _you_ decide what the
substitute should be (of course that can be 'none', 'nothing',
'undefined' or whatever, or maybe just 0, "", ... -- whatever suits your
needs.)
(And if you _must_ share that placeholder across several modules /
libraries / ..., you can have a simple global constant 'none = {}'
(possibly with a metatable to prevent '__index'/'__newindex' etc.), but
you definitely don't need to change the language!)
-- Marco
This is so well said.
I believe that when something is hard and the solution required an explosion of code, the situation should be shared on this list. Together, solutions may be discussed and your explosion cleaned up.
On rare occasions, there won't be a good solution, or the solution won't be satisfactory and that may be pointed out.
My goodness. Lua does not need another nil or empty or undefined value. We don't need === for that matter, either.
This is a really simple language. Sometimes you even have to perform some meta programming, in order to get it to do what you want. That's normal for Lua and it should be embraced as a part of its character--not rehashed over and over, only to realize that the road to perfection leads to complicated.
-Andrew