lua-users home
lua-l archive

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


Op Di., 18 Sep. 2018 om 22:29 het Eike Decker <zet23t@googlemail.com> geskryf:

> Interesting use case. I am still thinking about that and another reason why __lt / __le / __eq metamethods are converting to bool might be that the bool operators are expecting this - which brings up the point that adding an __and and __or metamethod would be useful too. This would give full control how objects should be handled when used in this context.

'and' and 'or' are not boolean operators, since they do not require
their operands to be boolean. The manual calls them "Logical
Operators", but they are not true operators because of the shortcut
evaluation. They should rather have been called logical directives
since they have more in common with "if...then" than with operators.

There are __band and __bor metamethods because there are operators '&" and "|".

At present, Lua always "knows what to do" with the logical directives,
so a metamethod, even if you had one, would never be called.

One could argue that the truthiness of a value (other than boolean or
nil) should be defined by metamehods instead of having one rule that
rings them all. It would certainly simplify the conversion of code
from Python and even C to Lua if we could define a __truth or
__toboolean metamethod for numbers that makes 0 falsy. But maybe call
the language that allows that Boa, not anything evoking Lua.