lua-users home
lua-l archive

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


Hello list,

I have an issue and I was wondering how you would solve it.

I tried to do something like this:

    local mp = require "MessagePack"
    local pretty = require "pl.pretty"
    pretty.dump(mp)

the Penlight code does something like:

    for _ in ipairs(mp.packers) do end

and it fails with:

    lua: /usr/share/lua/5.3/MessagePack.lua:46: pack '1' is
    unimplemented

because of a metamethod on the `packers` table [1].

How would you keep the feature of raising an error on direct access, but
still make it work with `ipairs`?

If possible, it would not use the debug library, e.g. not something
like:

    if debug.getinfo(2, "n").name == "for iterator"

It would not work anyway since MessagePack sets `_ENV` to `nil`...

In 5.2 it could have used `__ipairs` but it is deprecated now that
`ipairs` honors `index`.

[1]
https://github.com/fperrad/lua-MessagePack/blob/dc12b0175b9dd3a921e2c693cdeb929ac080ed4d/src5.3/MessagePack.lua#L45-L47

-- 
Pierre Chapuis