lua-users home
lua-l archive

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



On Sun, Jul 24, 2016 at 17:19 nobody <nobody+lua-list@afra-berlin.de> wrote:
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