我正在尝试创建一个类,它将存储一组时间序列的数据 - 按组进行组织,但我有一些编译错误,所以我剥离了基础知识(只是一个简单的实例化),仍然无法克服编译错误.我希望有人可能以前见过这个问题.Clas定义为:
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
Run Code Online (Sandbox Code Playgroud)
所以对我来说看起来没问题(它可能不是,但没有任何错误或警告) - 实例化得到错误:
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
Run Code Online (Sandbox Code Playgroud)
不推荐使用此构造:此处不允许使用类型语法"int C"和"C".考虑将此类型调整为以"C"形式编写
注意这可能是简单的愚蠢 - 我只是从假期回来,我的大脑仍处于时区滞后...
编译器只是说你需要在括号中包含构造函数的参数:
// the following should work fine
let q = new TimeSeriesQueue<tempRec, string>(keyFunc)
Run Code Online (Sandbox Code Playgroud)
还有一些其他问题 - 构造函数需要是公共的(否则你不能调用它),参数也keyFunc应该在括号中(否则,编译器会认为类型注释是函数的结果):
let keyFunc (r:tempRec) = r.someKey
Run Code Online (Sandbox Code Playgroud)
您还可以考虑使用隐式构造函数语法,这使得F#中的类声明更加简单.构造函数的参数自动在类的主体中可用,您可以使用以下命令声明(私有)字段let:
type TimeSeriesQueue<'V, 'K when 'K: comparison>(getKey : 'V -> 'K) =
let daysInCache = 7
let cache = ref Map.empty
member x.Foo() = ()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
351 次 |
| 最近记录: |