F # and op_GreaterThan

I recently tried to write something in F # using Microsoft Solver Foundation Services, and at the same time I ran into a problem: I had to express a condition (Term> Term), which in C # could simply be expressed as t1> t2 and returns another Term object. In F #, instead, I had to call Term.op_GreaterThan to achieve the same result as using t1> t2 would give bool, not a term.
Now I wonder why F # only selects op_GreaterThan if it gives a boolean? And what significance would the interpretation of F # t1> t2 have if the term does not implement IComparable?
Keep in mind that I understand why, when doing such a thing with equality and all equality based on the concept of structural comparison, I just donโ€™t understand how this can be extended to more than / less comparisons.

+4
source share
3 answers

Answer to the textbook:

Operator overloading is not part of the Common Language Specification, which means that compiler authors can ignore or only partially support it if they want. As a library writer, you are responsible for providing alternative ways for people to work with the class.

The pragmatic answer:

Because this is a stupid thing in the first place. The op_GreaterThan method was explicitly created for comparison. What it is, you should not do โ€œinterestingโ€ things with it, as if giving in to two terms. The CLR only allows you to abuse it, because it must support legacy languages โ€‹โ€‹such as C ++.

By the way, there is overload specifically for combining two things together. It is called op_Concatenate. You really should use it instead of op_GreaterThan.

EDIT

Almost good answer:

In F #, the concatenation operator I mentioned is ^.

I call this an almost good answer because I'm not sure if C # supports it. I think this is only allowed in VB and F #.

EDIT No. 2

It seems that F # does not honor overload ^.

EDIT No. 3

WTF going on here? F # does not comply with the> operator at all. Of course, you can overload it and it will correctly emit the op_GreaterThan method, but it ignores it. It doesn't even try to use op_GreaterThan, instead, it looks for the System.IComparable interface.

Worse, it is a runtime check. Despite the fact that it can statically determine that the Foo class does not implement IComparable, it still goes and compiles the code anyway.

+4
source

Intuitively, a comparison operator such as > should always give a boolean value!

You can answer the question a > b yes or no, but not with 3 or new Stopwatch() .

When he can return something else, something like

 if a < b then 

no longer makes sense.

F # objects (tuples, lists, etc.) often implement IComparable or IStructuralComparable , which can, for example, sort a tuple lexicographically.

* Note:

IMHO, this is not the best solution for comparing any objects and then throwing exceptions at runtime. Haskell solved this better using cool classes.

 greater :: (Ord a) => a -> a -> Bool greater ab = a < b 

Comparison of incomparable types will fail at compile time.

+2
source

You do not need to use the <and> operators. The Solver Foundation service model class uses the Bigger and Lesser methods, you should use them instead.

0
source

Source: https://habr.com/ru/post/1285916/


All Articles