dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.87k stars 783 forks source link

ValueTuple can't be cast to System.IComparable<ValueTuple> #10880

Closed charlesroddie closed 3 years ago

charlesroddie commented 3 years ago
open System

ValueTuple.Create(0,0) :> IComparable<ValueTuple<int,int>>

gives

error FS0193: Type constraint mismatch. The type 
    'struct (int * int)'    
is not compatible with type
    'IComparable<struct (int * int)>'

In the spec the types are compatible, and in C# this works fine:

var x = ValueTuple.Create(0, 0);
var y = (IComparable<ValueTuple<int, int>>) x;

F# reference tuples also fail, but this could be a missing feature rather than a casting issue:

(0,0) :> IComparable<int *int> // FS0193...
zpodlovics commented 3 years ago

I have similar issues with struct tuples and ITuple interface here: https://github.com/dotnet/fsharp/issues/5654 However there is a "workaround" to make it work.

(box (System.ValueTuple.Create(1,2)) :?> System.IComparable<System.ValueTuple<int,int>>);;
KevinRansom commented 3 years ago

Feeling pretty smug right now :-)

> ValueTuple.Create(37,300.2) :> IComparable<ValueTuple<int,double>>;;
val it : IComparable<struct (int * double)> = struct (37, 300.2)