Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# tuple, System.Tuple and collection - Type constraint mismatch?

Tags:

f#

The last two lines in the following code got the compiler error?

open System

let s = new System.Collections.Generic.Stack<Tuple<int, int>>()
s.Push( 1, 2) // The type ''a * 'b' is not compatible with the type 'Tuple<int,int>'
s.Push(Tuple.Create(1, 2))

The error message of the s.Push(Tuple.Create(1, 2))

Type constraint mismatch. The type 
    ''a * 'b'    
is not compatible with type
    'Tuple'
The type ''a * 'b' is not compatible with the type 'Tuple'
type Tuple =
  static member Create : item1:'T1 -> Tuple + 7 overloads
Full name: System.Tuple
like image 839
ca9163d9 Avatar asked Oct 18 '22 12:10

ca9163d9


1 Answers

Though F# tuples are represented with System.Tuple in compiled form, they are not considered an "alias" for it from the logical language standpoint.

int * int is not the same as System.Tuple<int, int> as far as F# compiler is concerned.

Just use the F# tuple syntax for the Stack generic argument, and it will work:

let s = new System.Collections.Generic.Stack<int * int>()
s.Push( 1, 2)
like image 108
Fyodor Soikin Avatar answered Oct 21 '22 06:10

Fyodor Soikin