Is there any way to determine whether a given type parameter satisfies the F# comparison constraint through reflection?
I would suspect not, since the expression
typedefof<Set<_>>.MakeGenericType [| typeof<System.Type> |]
appears to yield no errors. Still, I would like to hear some authoritative opinion on this.
Quoting from Don Syme’s thorough post on equality and comparison constraints:
The constraint type : comparison holds if:
The constraint
'T when 'T :> IComparablecan be encoded in CIL and reflected upon, whereas neither is true of'T when 'T : comparison.Since the two constraints are not equivalent, marking
comparabletypes with theIComparableconstraint is a bit misleading since it would make it impossible to distinguish between the two using reflection.There’s a similar relationship between the
equalityconstraint andIEquatable<_>.EDIT
Jack’s mention that the
comparisonconstraint could be encoded in F# metadata prompted me to look at the metadata reader in PowerPack. It can be used to detect the constraint:Here’s a contrived example that shows the disparity between implementing
IComparableand satisfyingcomparison: