lua-users home
lua-l archive

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

> Actually, the re-use of the square brackets is my biggest objection to
> a quaternany operator.
> One of the best things about Lua is that it doesn't re-use symbols
> much.  Every other lanugage I'm aware of (except the Lisp family) does
> this, and I believe it makes learning those languages more difficult.
I don't intend to die for this syntax but I couldn't find a better way
to implement a C-like conditional operator without introduce a new
symbol. Any alternative syntax is welcome :-).

> With Lua, it is real simple.  This is easy for the user, and for the
> lex / parse.  You may scoff at the parsing advantage, but it also
> makes syntax errors easier to decipher, because the compiler has a
> clear idea of what it was expecting next.
Well, although is not the same thing, Lua already uses '[' to strings
'[[' and comments '--[['. So the Square Bracket already is a versatile
symbol that could be used to express another idea.

Ok, today is possible to detect the kind of SB usage just after the
open bracket, but with QOP the difference can only be seen after
parsing the expression.  Example:

a = b [ very_long_function_name_with_parameters(param1,param2,param3); 1; 2]

The compiler will detect the conditional operator only when scans the
first semicolon and will generate a error  "You cannot use a
conditional operator here".  In fact, this is not a big problem
because to be used as conditional operator, the square bracket must be
inside a exp, like this (5.2):

a = b; ([ very_long_function_name_with_parameters(param1,param2,param3); 1; 2])

The interesting case is:

a = { [ exp; 1] }

Lua only knows if the open square bracket means a index or a
conditional operator after the semicolon.

It might be seen as a new "feature" for Lua - a kind of  **conditional
table insertion**.

Basic idea:

First Lua evaluates the condition then prepare itself  to store the
evaluation of the selected option. BUT if the evaluated value is NIL,
Lua does not store the value and therefore DOES NOT increment the
index count.  Example:

local a = false -- or nil
t = {   1; [a; 2]; 3 }
print( t[1], t[2] )
>>   1  3

I think that today, there is no way to produce the same result without
creating a initial table with NILs and then creating a final table
removing the nil values, which spends more memory and CPU (to pack and
to collect the initial table).

> Lua can get away with this because it has just one built in container.
> Compare this to Python, which has three types of containers, and can
> be confusing for the beginner.

This is one of the reasons because I like Lua so much and because I
really believe that Lua - with some improvements - could be used
beyond of the script language's niches.