lua-users home
lua-l archive

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

On 08/09/2013 16.47, Seppl Vorderhörer wrote:
> Am 08.09.2013 13:14, schrieb Lorenzo Donati:
>> Well, I like the and/or idiom and I accept the term "hack" only because
>> it fails when "false/nil" comes into play...
> Really?
> To write a statement   "a and b or c"  when you in fact want "if a is
> true then use b otherwise c" was one of the weirdest things I've ever
> seen so far.
Well, I didn't say it is the perfect syntax for the job. As I said
later, I sometimes play with the thought of having a more verbose:

a = if x > 0 then let "hello" else let "argh" end

instead of

a = x > 0 and "hello" or "argh"

But I don't know if it's worth the fuss. First because it's a mechanism
that would never be accepted by Lua team since it's not general, and
second because even if this "let expr" syntax were generally applicable
to any statement (e.g. loops), I fear that it would not bring so many
benefits to justify its introduction.

As for its readability, once I got into it I found it more readable than
the C ternary operator, which is akin to line noise to me. Try to put
more complex expressions into the game and compare:

a = ( x^2 + 1 > 0 ) and (y - 2)^(x + 1) or delta + 1

a = ( x^2 + 1 > 0 ) ? (y - 2)^(x + 1) : delta + 1

IMHO those ? and : get swamped by the other symbols and are easier to
miss when skimming through code compared to "and/or" keywords (YMMV).

> An expression " a ? b : c " where it's explizitly defined and clear what
> happens and what the result is would be way clearer...
> (... at least in my opinion.)

()  ascii ribbon campaign - against html e-mail
/\   - against proprietary attachments