lua-users home
lua-l archive

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


On Wed, Mar 14, 2018, at 15:55, Roberto Ierusalimschy wrote:

> Every year or so the list has its recurrent discussion about the length
> operator and holes in sequences. Quite often, this discussion leads some
> people to propose that Lua needs an array type, different from tables.

In practice, this is not the reason why people (at least me, but not only)
wish Lua had an array type.

The main reason is that Lua is a dynamic language where you can
inspect the type of values (with type() mostly), but there is no
standard way to know if a table is supposed to be a sequence
or not when you get it. But in the outside world, things are not
tables, they tend to be sequences or map. This causes issues
when we try to interact with it using Lua.

I said it's not only me because I had feedback on that just yesterday
on Twitter [1] in a thread I started asking why Lua ended up in the list
of "most dreaded languages" in the 2018 Stack Overflow study...

In my own case, here is the kind of trick I have to resort to to deal
with this when using, for instance, François Perrad's MessagePack
library [2]:

    local mp = require "MessagePack"

    rawset(mp.packers, "function", function(buf, f) f(buf) end)

    function mp.MAP(t)
        return function(buf)
            local n = 0
            for _ in pairs(t) do n = n + 1 end
            mp.packers["map"](buf, t, n)
        end
    end

    function mp.ARRAY(t)
        return function(buf)
            mp.packers["array"](buf, t, #t)
        end
    end

    local my_binary = mp.encode {
        an_empty_array = mp.ARRAY {},
        an_empty_map = mp.MAP {},
        -- ... etc
    }

In another dynamic language (Ruby, Python, JavaScript, etc) this code
would look like this instead:

    local mp = require "MessagePack"

    local my_binary = mp.encode {
        an_empty_array = [],
        an_empty_map = {},
    }

Sure, the library itself could provide a helper and it could look like this:

    local mp = require "MessagePack"

    local my_binary = mp.encode {
        an_empty_array = mp.sequence(),
        an_empty_map = {},
    }

but even if it did, that does not solve the issue of structures created
by code that doesn't even know about the library.

For instance, this is dangerous:

    local mp = require "MessagePack"
    local json = require "json"

    local my_json = get_json()
    local my_binary = mp.encode(json.decode(my_json))

whereas in another language it would be fine (disregarding error
handling etc, this is an example).

[1] https://twitter.com/karlseguin/status/973582788950573058
[2] https://github.com/fperrad/lua-MessagePack

-- 
Pierre Chapuis