以下代码中的最后两行是否出现编译错误?
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
答案 0 :(得分:3)
虽然F#元组以编译形式用System.Tuple
表示,但它们不被视为&#34;别名&#34;从逻辑语言的角度来看它。
int * int
与System.Tuple<int, int>
不同。
只需使用Stack
泛型参数的F#元组语法,它就可以工作:
let s = new System.Collections.Generic.Stack<int * int>()
s.Push( 1, 2)