问题
I want to sort a sequence of Version
objects in F#:
let maxVersion =
versions
|> Seq.max (fun version -> version)
The compiler produces the following error message:
The type '(seq -> 'a)' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface
When I hit F12 in Visual studio to take a look at the metadata of Version
it says that Version
only implements ICloneable
, but not IComparable
. But when I go to sourceof.net it says it implements IComparable
as well as some other interfaces.
Does F# use a different version of the .NET framework?
回答1:
The error message is telling you that (seq->'a)
does not implement IComparable
which is true since (seq->'a)
is a function, not a sequence.
If you look at the signature of Seq.max
it takes only the sequence as parameter.
Remove the lambda (fun version -> version)
and it should be alright.
Otherwise, if you want to apply a key generator function for the sort, use instead Seq.maxBy
来源:https://stackoverflow.com/questions/27524101/system-version-doesnt-implement-system-icomparable-in-f