lua-users home
lua-l archive

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


Hi,

> >>>1) Why is only one function declaration syntax supported in tables?
> >>Sounds like a bug to me.
> >I don't think it's a bug, but a parser's restriction.
> 
> OK, here's the deal (I think).  Lua's syntax has lots of "sugar" and this
> case piles "sugar" on top of "sugar" and that's not healthy.

I do not see a conflict. It's straightforward to extend the parser to handle
all cases:

 > t = { function() end; a = function() end; function b() end }
 > table.foreach(t, print)
1       function: 0x8087198
a       function: 0x80a1b58
b       function: 0x80a1b78

[This was run with a patched copy of Lua.]

> There, now I've explained why its not a trivial bug, but one that has
> probably been hit numerous times, but not worth anyone's efforts to fix as
> its much easier to just redefine it.

Some time ago I've written a simple patch for the parser to handle named
functions as fields inside table constructors. I can put it up on the Wiki
if anyone wants it really badly.

But ... I've abandoned it since. Nowadays I prefer to use this way:

local foo = {}
function foo.abc() end
function foo:xyz() end

On several occasions I had the need to conditionally add fields to a
table. Of course this is not possible with pure constructor syntax.
So for consistency I do not use this syntax anymore (but I admit it was
a convenient and intuitive extension to the syntax).

Bye,
     Mike