lua-users home
lua-l archive

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


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