Generics 具有非泛型构造函数的泛型F#类型
有没有办法让泛型类型具有创建非泛型实例的无参数构造函数?非编译示例:Generics 具有非泛型构造函数的泛型F#类型,generics,f#,Generics,F#,有没有办法让泛型类型具有创建非泛型实例的无参数构造函数?非编译示例: type Child<'T>() = class end type Parent<'T>(c:Child<'T>) = new() = Parent(Child<unit>()) // ERROR: This code is less generic than required by // its ann
type Child<'T>() = class end
type Parent<'T>(c:Child<'T>) =
new() = Parent(Child<unit>()) // ERROR: 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 'unit'.
键入Child(c:Child太诚实了,我想你真的在寻找一个可选参数
type Parent<'T>(?c:Child<'T>) = class
end
type Parent)=类
终止
当然,这需要他们指定类型,但这真的很糟糕吗
let p = Parent<int>()
设p=Parent()
老实说,我认为您确实在寻找一个可选参数
type Parent<'T>(?c:Child<'T>) = class
end
type Parent)=类
终止
当然,这需要他们指定类型,但这真的很糟糕吗
let p = Parent<int>()
设p=Parent()
您想要的是不可能的-调用泛型对象的构造函数时,调用方始终可以指定他想要的任何类型参数。这类似于调用静态方法-调用方始终可以指定类型:
let a = new Parent<Foo>()
let b = Parent<Foo>.Bar
然后你可以写:
let a = Parent.New()
let b = Parent<Foo>.New() // You can specify type parameter, but it is ignored,
// because return type is always 'Parent<unit>'
a=Parent.New()
设b=Parent.New()//您可以指定类型参数,但它被忽略,
//因为返回类型始终为“父”
您想要的是不可能的-调用泛型对象的构造函数时,调用方始终可以指定他想要的任何类型参数。这类似于调用静态方法-调用方始终可以指定类型:
let a = new Parent<Foo>()
let b = Parent<Foo>.Bar
然后你可以写:
let a = Parent.New()
let b = Parent<Foo>.New() // You can specify type parameter, but it is ignored,
// because return type is always 'Parent<unit>'
a=Parent.New()
设b=Parent.New()//您可以指定类型参数,但它被忽略,
//因为返回类型始终为“父”