Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merely implementing the generic IComparable interface with CustomComparisonAttribute does not satisfy the "comparison" constraint. #7945

Closed
teo-tsirpanis opened this issue Dec 7, 2019 · 1 comment

Comments

@teo-tsirpanis
Copy link
Contributor

I think the title is descriptive enough.

Code sample:

open System

[<StructuralEquality; CustomComparison>]
type MyType = MyType of int
with
  member x.Value = match x with MyType x -> x
  interface IComparable<MyType> with
    member x.CompareTo(x') =
      compare x.Value x'.Value

let mySet = Set.singleton <| MyType 521

At MyType 521, it presents the following compiler error:

error FS0001: The type 'MyType' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface

MyType however implements the generic IComparable. Why isn't that enough? Shouldn't the compiler provide an automatic implementation for the non-generic interface in cases like that?

On the other hand, the generic IEquatable interface is sufficient for the "equality" constraint.

@cartermp
Copy link
Contributor

cartermp commented Dec 7, 2019

This is currently by design - the comparison constraint is satisfied by the System.IComparable interface, not the System.IComparable<'T> interface. I recommend filing a new language suggestion here to propose that the generic interface do this: https://github.com/fsharp/fslang-suggestions/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants