2
type Foo(size: int,data: 'T []) =
new(data: float []) =
Foo(sizeof<float>,data)
new(data: int []) =
Foo(sizeof<int>,data) //error: f# thinks that data is float []
member this.Size() = size
基本上我需要几个带有泛型数组'T []的构造函数,我只关心'T'的大小。F#中的通用构造函数#
我想用这样的:
Foo([|1,2,3,4|]).Size() // prints size of int
Foo([|1.f,2.f,3.f,4.f|]).Size() // prints size of float
我将如何做到这一点?
UPDATE1:
我才意识到,我不能让编译器推断大小,我必须手动完成。
type Foo<'T>(size: int,data: 'T []) =
new(data: float []) =
Foo(4,data)
new(data: int []) =
Foo(16,data)
new(data: Vector3 []) =
Foo(Vector3.SizeInBytes,data)
member this.Size() = size
会这样吗?
当我做 Foo([|new Vector3(1.f,1.f,1.f)|]
我想美孚是的Foo<Vector3>
,因此数据应该是数据类型:的Vector3 []