lua-users home
lua-l archive

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


On Sun, Jul 24, 2016 at 6:18 PM, nobody <nobody+lua-list@afra-berlin.de> wrote:
> 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!)

Except that there's no way to make that substitute value falsey. Thus,
code testing for the lack of a useful value must use explicit equality
tests, rather than simply 'if var then ...', which makes for uglier
code.

I'd rather have a falsey value that's not literally 'false', and for
that, a change to the Lua language is required (be it '__bool' or
'undef').