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'.
I don't see a problem with not having a __gt / __ge metamethod, it currently simply swaps the operators when calling the __lt / __le methods - which is fine for symmetric operations. The missing __not metamethod is probably a good reason why it works how it does right now.
It feels unsatisfying though. Having a few more metamethods instead and going through a greater length of implementation would be not the problem in the average case I guess.
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?
Abusing operators came to my mind too, but I was looking more for an explanation why the current implementation works the way it does.
Cheers,
Eike