lua-users home
lua-l archive

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


Hi, list!

I'm glad table.pack() is here, as it is one of the functions I always
write myself.

5.2 pack() could be defined as follows:

    function pack(...)
      return { n = select("#", ...), ... }
    end

We need to store number of arguments, to be able to call unpack()
safely if arguments contain nils (or table size definition will get
us).

Today I looked at my code, and realized that my pack has slightly
different protocol:

    function pack(...)
      return select("#", ...), { ... }
    end

Note that it is probably better to swap size and table:

    function pack(...)
      return { ... }, select("#", ...)
    end

So user who do not need size, would not be forced to create a variable for it:

    local t = pack(...)

I do not argue for this protocol just because it is mine — it is not a
problem to switch to a new one.

That being said, I wonder, which one is better and why... What do you think?

Alexander.