System.Version doesn't implement System.IComparable in F#

醉酒当歌 提交于 2020-01-15 08:29:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!