lua-users home
lua-l archive

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


On Sun, Feb 23, 2014 at 9:49 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2014-02-23 2:38 GMT+02:00 Anders Petersson <andpet@gmail.com>:

> I didn't receive the Programming in Lua book yet so this is all from online
> sources.

PiL 3 is available online, though not gratis.

Oh I didn't see that! Bought it at once.

> I'm surprised there's no built-in function to print the contents of a table.
> I looked at lua-stdlib [2] but it doesn't define such a function either.

Because tables can be printed in so many ways, there is no
one-size-fits-all.

For the beginner, any print function would help. For example, in Python 'dir()' can show what functions are available.

> In particular I haven't found a good description of syntax and semantics of
> __pairs/__ipairs metamethods.

Those in the official reference manual are good, though terse. See
under global functions `pairs` and `ipairs`. But the really decent
explanation is in PiL.

Here's the explanation of pairs() in the manual:
"If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.
Otherwise, returns three values: the next function, the table t, and nil, so that the construction
     for k,v in pairs(t) do body end
will iterate over all key–value pairs of table t."
That doesn't tell me what the three return values do.

> The wiki [4] talks of 'pairs' as an experimental feature and also mentions
> 'next' which wasn't even included in 5.2? I think the page isn't updated for 5.2?

A wiki is written by users. There is the occasional contributor that
has a sense of ownership and occasionally revises his pages, but
by and large, when you put something on a Wiki, it is there for
everybody to modify and correct. Even newbies with two weeks
of experience.

I'm genuinely unsure what the status of the information on that page is. Until I learn more or someone else volunteers, I've added a small warning at the top of the page.

> These metamethods are not listed in the 5.2 manual:
> __mode, __pairs, __ipairs, __gc, __metatable, __tostring

They don't have their own entries, but search and you shall find.

With all respect, I ask the maintainers of the manual to add a specific entry for each metamethod under section 2.4 'Metatables and metamethods'.

> And 6.2 'Coroutine Manipulation' states that coroutine operations "comprise
> a sub-library of the basic library", which is no longer true.

Why is it not true? There is a "coroutine" sublibrary.

You know better than me if that's the case. I found this remark on http://lua-users.org/wiki/LuaFiveTwo. If it's not true, I volunteer to edit the wiki page.


> Also, 6.4.1 'Patterns' doesn't explain the now official %f pattern.

What version of the manual do you have? The one that comes
with Lua 5.2.3 does explain the frontier pattern.

Ok, I see it now. My mistake.

I like the upcoming change to '=' syntax in Lua 5.3 and the discussion on a default table print function is promising. I'll look into ilua.
Thanks and best regards
/Anders Petersson