F#元组,System.Tuple和集合 - 类型约束不匹配?

时间:2017-05-09 18:44:54

标签: f#

以下代码中的最后两行是否出现编译错误?

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))

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

1 个答案:

答案 0 :(得分:3)

虽然F#元组以编译形式用System.Tuple表示,但它们不被视为&#34;别名&#34;从逻辑语言的角度来看它。

就F#编译器而言,

int * intSystem.Tuple<int, int>不同。

只需使用Stack泛型参数的F#元组语法,它就可以工作:

let s = new System.Collections.Generic.Stack<int * int>()
s.Push( 1, 2)