lua-users home
lua-l archive

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


On 27 July 2017 at 07:40, Pierre Chapuis <catwell@archlinux.us> wrote:
> 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`.

On the Penlight side, it would probably be a good to protect the
indexing of this generic traversal with pcall.

On the MessagePack side, if a `packer` called 1 does not make sense,
I'd add an extra `if k == 1 then return nil end` just to appease
ipairs.

-- Hisham