lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
>
> > 1. Why does this not work?
> >
> > a = { 1, 2, 3, 4 } [1] --> Error: unexpected symbol near '['
> >
> > But this does:
> >
> > a = ({ 1, 2, 3, 4 }) [1]
>
> That would create ambiguities in the next fragment:
>
>   a = i
>   {f, g, h}[1]("hi")
> 
> It could mean any of these:
> 
>   a = i; {f, g, h}[1]("hi")
>   a = i{f, g, h}[1]("hi")
> 
> Forcing the use of parentheses reduce the cases of ambiguity.

That's a point I never found a good reason for: why do you
require parentheses for expressions like the ones from the OP?

Is it just to allow parentheses to start a statement?!?  Just
drop that (imho mis-)feature.  It creates ambiguities (just
look at that "ambigious syntax" error that requires function
calls to conform to a strange whitespace rule), makes regular
expressions have to use superfluous parenthesis and makes it
hard to detect statement beginnings.

A simple rule: a statement begins with either a reserved word
(for, while, etc) or an identifier. Point.

No ambiguities any more and you can use such nice constructs
like

	print("Foo %x bar %.3f baz":format(1,2))

without the need for these "useless" parentheses.  The OPs ex-
ample would work too, of course.

And if you really need to start a statement with some expression,
use the identity function (function id(...) return ... end):

	id{a,b,c}[i](args)

instead of

	({a,b,c})[i](args)

[Btw, I can't remember that I ever actually used this construct...]

And, if I remember correctly, such a change even simplifies the
parser.

Ciao, ET.