lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Operator annotations for comparison operators

Open CelticMinstrel opened this issue 3 years ago • 3 comments

The @operator annotation supports nearly all the operator metamethods, except for eq / lt / le. I'd like to be able to annotate that my type are comparable.

CelticMinstrel avatar Feb 06 '23 06:02 CelticMinstrel

I remember why I didn't support these operators. These operations will be adjusted to Boolean values no matter what type of metamethod you returns. Therefore, it is meaningless to declare these operators.

sumneko avatar Feb 13 '23 06:02 sumneko

But there's still a difference between values that can be compared and values that can't (at least with the relational comparisons – < <= > >=). While all types can be compared with == ~= (and those with __eq metamethods just invoke custom behaviour when doing so), not all types can be compared with < <= > >=. So I'd like a way to indicate that.

There appears to be two possible states in Lua 5.4. A type that implements __lt also gets __le for free even if it didn't explicitly implement it, so it can be compared with all of < <= > >=. On the other hand, a type that implements __le but not __lt can be compared with <= >= but not with < >. I can't think of any real use-case for this second case, however.

So if you don't like declaring these using @operator, perhaps a new @comparable annotation could be used? It could either be @comparable with no arguments, or @comparable other_type meaning that you can compare values of this type to values of the other type. You could ignore == ~= since they have a default effect even for types that don't implement __eq (comparing by reference).

CelticMinstrel avatar Feb 13 '23 07:02 CelticMinstrel

related discussion #2750

tomlau10 avatar Aug 08 '24 11:08 tomlau10