在以下代码中,请注意get_Zero的类型约束:
type Wrapper<'t> = { Data : 't[] }
let compute<'t
when 't : (static member get_Zero : unit -> 't)
and 't : (static member (~-) : 't -> 't)
and 't : (static member (+) : 't * 't -> 't)>
(wrapper : Wrapper<'t>) =
wrapper.Data
|> Seq.mapi (fun i value -> (i, value))
|> Seq.sumBy (fun (i, value) ->
if i % 2 = 0 then value
else -value)
即使我已经有一个显式的类型约束,我仍然在调用Seq.sumBy时遇到以下编译器错误:
当^ t:(静态成员)时,类型参数缺少约束 get_Zero: - &gt; ^ T)'
任何人都知道这里发生了什么?感谢。
答案 0 :(得分:7)
尝试使下游静态成员约束显式化可能是一种挫败感,幸运的是,它很少需要。只需标记函数inline
并让它们被推断出来。
let inline compute (wrapper : Wrapper<_>) =
wrapper.Data
|> Seq.mapi (fun i value -> (i, value))
|> Seq.sumBy (fun (i, value) ->
if i % 2 = 0 then value
else -value)
正确的签名是:
let inline compute<'t
when 't : (static member Zero : 't)
and 't : (static member (~-) : 't -> 't)
and 't : (static member (+) : 't * 't -> 't)>
(你会注意到错误信息中的签名甚至不是有效的语法:when ^t : (static member get_Zero : -> ^t)
。这是我沮丧的意思的一部分。)