lua-users home
lua-l archive

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


Roberto Ierusalimschy escribió:

> The following code does the trick you want:

>   a[x] = function_which_might_return_false(x) or nil

Yes, I thought of that (although it's a little odd, don't you think?
Wouldn't a ? operator that "corrected" false to nil be more perspicuous :-)
but I digress).

Nonetheless, I think there are other cases where if establishing a local
block would be useful, and surely it is not a major change to the language?

To Victor Bogado da Silva Lins:

Maybe I didn't make the suggestion clear enough. I am not proposing making
= an operator, Perl style. There is no value returned by the expression;
the if statement would simply test the result of the first assignment
(which is not the same as being an operator: consider the for statement).

The problem with "false" is not in if statements; rather it is in the
assignment to tables. False is *not* compatible with nil, because setting a
table key to false means that it the key will be present in the table,
whereas setting it to nil means that it will not be present. If you are
going to use table iteration to do something on keys that are present, the
ambiguity is a serious problem (once of the problems Perl has, for example:
key existence is a little hard to predict.)

R.