2010-05-08 96 views
0

我想创建一个类,将存储数据的时间序列 - 按组织组织,但我有一些编译错误,所以我剥离到基础(只是一个简单实例化),仍然无法克服编译错误。我希望以前有人可能看到过这个问题。柯乐的定义是:F#类与泛型:'构造函数不推荐'错误

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 

This construct is deprecated: The use of the type syntax 'int C' and 'C ' is not permitted here. Consider adjusting this type to be written in the form 'C'

注意这可能是简单的愚蠢 - 我只是从假期回来,我的大脑仍然在时区滞后...

回答

7

编译器只是说你需要用括号括起来的构造函数的参数:

// the following should work fine 
let q = new TimeSeriesQueue<tempRec, string>(keyFunc) 

还有一些其他的问题,但 - 构造函数必须是公共的(否则就不能称之为)和keyFunc参数也应该是在括号(否则,编译器会认为类型标注是函数的结果):

let keyFunc (r:tempRec) = r.someKey 

您也可以考虑使用隐式构造函数的语法,这使得类声明在F#简单得多。构造函数的参数在类的身体会自动变为可用,你可以声明(私人)领域简单地使用let

type TimeSeriesQueue<'V, 'K when 'K: comparison>(getKey : 'V -> 'K) = 
    let daysInCache = 7 
    let cache = ref Map.empty 

    member x.Foo() =() 
+0

我也注意到与keyFunc错字 - 必须满足以下条件,因为我是简化了我的代码帖子。我确实在实例化过程中添加了parens,它只是产生了第二个编译错误: “Method or object constructor'TimeSeriesQueue'2'not found” 我不反对将类语法改为隐式 - 但我应该能够解决它明确以及... – akaphenom 2010-05-08 13:35:24

+0

没关系...度假rediculousness,构造者被标记为私人... – akaphenom 2010-05-08 13:37:24