【发布时间】:2021-02-25 06:38:17
【问题描述】:
今天我注意到以下内容无法编译:
open System
type MyType() =
member this.Something() =
this.F(3)
this.F("boo")
// ^^^^^
// This expression was expected to have type 'int' but here has type 'string'
member private this.F<'T> (t:'T) =
// ^^
// This type parameter has been used in a way that constrains it to always be 'int'
// This code is less generic than required by its annotations because the explicit type variable 'T' could not be generalized. It was constrained to be 'int'.
Console.WriteLine (t.GetType())
但是只要改变申报顺序,就没有问题了。
open System
type MyType() =
member private this.F<'T> (t:'T) =
Console.WriteLine (t.GetType())
member this.Something() =
this.F(3)
this.F("boo")
我花了很长时间才弄清楚,因为我没想到声明顺序对班级成员很重要。这是预期的行为吗?
【问题讨论】:
标签: f#