任何人都可以解释为什么下面的第二个例子不会编译?为什么'代码不够通用'?
'测试2'给出了“错误FS0670:该代码不够通用,类型变量^ a不能泛化,因为它会逃避它的范围。”我无法理解此错误消息。
// Test 1
type test1<'a> = | A of 'a
with
override t.ToString() =
match t with
| A a -> a.ToString()
// Test 2
type test2<'a> = | A of 'a
with
override t.ToString() =
match t with
| A a -> string a
// Test 3
type test3<'a> = | A of 'a
with
override t.ToString() =
match t with
| A a -> string (a :> obj)
'string' [事实上是'T - > string'](http://msdn.microsoft.com/en-gb/library/ee340491.aspx),所以这应该在理论上工作。尽管我没有更好的解释。 – 2010-07-09 09:22:41
哦,是的,我只是卡住了字符串;;到F#交互式来获取类型。 推测实际上使用字符串是强制编译强制输入类型,它选择obj作为最低公分母? – Massif 2010-07-09 09:56:48