Class 具有泛型的F类:“构造函数已弃用”错误
我试图创建一个类来存储时间序列数据-按组组织,但我有一些编译错误,所以我只进行了简单的实例化,但仍然无法克服编译错误。我希望有人以前见过这个问题。Clas的定义如下:Class 具有泛型的F类:“构造函数已弃用”错误,class,f#,generics,Class,F#,Generics,我试图创建一个类来存储时间序列数据-按组组织,但我有一些编译错误,所以我只进行了简单的实例化,但仍然无法克服编译错误。我希望有人以前见过这个问题。Clas的定义如下: type TimeSeriesQueue<'V, 'K when 'K: comparison> = class val private m_daysInCache: int val private m_cache: Map<'K, 'V list ref > ref;
type TimeSeriesQueue<'V, 'K when 'K: comparison> = class
val private m_daysInCache: int
val private m_cache: Map<'K, 'V list ref > ref;
val private m_getKey: ('V -> 'K) ;
private new(getKey) = {
m_cache = ref Map.empty
m_daysInCache = 7 ;
m_getKey = getKey ;
}
end
因此,在我看来,这是正常的,可能不是,但没有任何错误或警告-实例化得到错误:
type tempRec = {
someKey: string ;
someVal1: int ;
someVal2: int ;
}
let keyFunc r:tempRec = r.someKey
// error occurs on the following line
let q = new TimeSeriesQueue<tempRec, string> keyFunc
此构造已弃用:使用
类型语法“int C”和“C”
“在这里是不允许的。考虑
调整要写入的此类型
表格‘C’
请注意,这可能是一个简单的愚蠢行为-我刚度假回来,大脑仍然处于时区滞后状态…编译器只是说需要将构造函数的参数括在括号中:
// the following should work fine
let q = new TimeSeriesQueue<tempRec, string>(keyFunc)
您还可以考虑使用隐式构造函数语法,使类声明更加简单。f构造函数的参数在类的主体中自动可用,并且可以简单地使用Le::/P>声明私有字段。
我确实注意到了keyFunc的输入错误——一定是在我简化代码以发布时发生的。我确实在实例化周围添加了参数,但它只是生成了第二个编译错误:未找到方法或对象构造函数“TimeSeriequeue`2”.我不反对将类语法更改为隐式-但我应该也能够显式解决它。。。
let keyFunc (r:tempRec) = r.someKey
type TimeSeriesQueue<'V, 'K when 'K: comparison>(getKey : 'V -> 'K) =
let daysInCache = 7
let cache = ref Map.empty
member x.Foo() = ()