lua-users home
lua-l archive

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


On 15/09/2018 01:00, Andrew Gierth wrote:
Ternary (conditional) operator. Using (x and y or z) has become an
idiom, which shows the real demand for such a feature, but the fact that
it breaks when y is false or nil makes for a lot of landmines.

I don't find it so problematic, but I agree that newbies could trip on that.

Adding that specific feature could be against the minimalistic nature of Lua (too much restricted a use case). But maybe a more general expression-like construct could be generally useful (dumb syntax below, warning! Just to get the point):

x = if (value) then exp1, exp2, ... expN; (I know it is not valid syntax)

or maybe:

x = ? value : exp1, exp2, ... expN  (shorter but probably more confused)

It's semantically like "select", but not a function call, so amenable to more optimization. And moreover is more explicit.


Something like #... to return the number of varargs without needing to
pass them all to another function as in select('#',...).


Yep!!! This was a pet peeve of mine for ages.

I generally hate `select` (but I love its functionalities). And I love varargs: if used with a grain of salt they make the code flexible and still readable.

Maybe #@ to indicate the number of varargs and @[i] the i-th vararg ("@" can be read as an "A"/"at", so it makes sense).

Unfortunately, I think I remember Roberto "hates" varargs and he wanted to get rid of them. So this "select as operator" has few chances to see the light (I'm still hoping he changes his mind, maybe if people on the list come up with a nice syntax with general application - maybe with expanded semantics... :-)


Maybe the two features before could be merged: make "@" a "select" operator, then:

x = @[i](exp1, exp2, ...., expN) is the i-th expression in the list

x = @[condition](false_expr, true_expr) is like a ternary operator (unintuitive the order of the results, though)

This could allow use of varargs like this:

x = @[i](...)

n = @#(...)	number of elements in the list


But all this starts to seem like t-uples are added to the language, but Roberto don't like this either (IIRC).

Maybe everything could be simplified if Lua provided a way to "declare" fixed-length array tables, so that:

{...}

could be optimized and "#" could be made return exactly the number of elements ("holes" included) and normal table access with "[i]" would do the rest. But now I'm just rambling. :-)


Cheers!

--Lorenzo