Skip to content

Satisfy the "comparison" constraint with IComparable<'T> #816

@teo-tsirpanis

Description

@teo-tsirpanis

I propose we allow a class that implements just the generic IComparable<'T> interface, satisfy the comparison constraint.

The existing way of approaching this problem in F# is by implementing the non-generic interface IComparable (or IStructuralComparable). However this approach is not ideal, because it requires a type cast (and potentially unboxing) of a value of type obj. Implementing all three interfaces would increase boilerplate code even more.

Pros and Cons

The advantages of making this adjustment to F# are increased ease of defining a custom comparison function, and conformity to modern coding practices by using generic interfaces.

The disadvantages of making this adjustment to F# are none, I guess. As for backwards compatibility, we can provide an automatic implementation of the two non-generic interfaces, if there are not specified.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: The issue was first reported in dotnet/fsharp#7945.

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions