lua-users home
lua-l archive

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


Op Ma., 17 Sep. 2018 om 09:23 het Eike Decker <zet23t@googlemail.com> geskryf:

> There are other cases where it would be useful to be able to return objects. For instance a vector math library could define that the comparison operator compare each scalar value individually and returns a new vector containing only 0/1 values.
>
> But currently, this is not possible (it seems?).
> Is there any particular reason why it is important to convert returned values that I don't see?

Yes, there is. One can deduce the existence of the reason from the
explcit sentence "The result of the call is always converted to a
boolean. " in the manual.

That does not mean that anybody outside the Lua team knows the reason,
but here is a guess.

There is no '__ne', '__ge' or '__gt': they are defined implicitly via
"__not" and are therefore always boolean. It would be very illogical
to break the identity 'a<b == b>a'.

Nothing stops you from using as a metamethod a function defined to
return a non-boolean, which you can always call directly. but that
does not solve the problem of your desire to (ab)use the comparison
operators.

Look, the notation "a<b" to mean a non-boolean is so counterintuitive,
why not use just say "a|b" which can return whatever? Or have you used
up all the binary operators already?

If all you need to do is to make booleans behave like 0-1 variables,
make a metatable for booleans like Lua 5.4 has one for strings.