【发布时间】:2015-04-07 14:23:31
【问题描述】:
我想为我的通用函数添加 2 个约束。一个构造函数约束和一个成员约束。
let somefn<'T when 'T : (new : unit -> 'T) and 'T : (member Add : 'T -> unit)> v = new 'T()
但是我收到以下错误:
/stdin(32,5): error FS0670: This code is not sufficiently generic.
The type variable ^T when ^T : (new : unit -> ^T) and
^T : (member Add : ^T * ^T -> unit)
could not be generalized because it would escape its scope.
我真的不明白这个错误信息。
仅当我将定义更改为:
let inline somefn<'T when 'T : (new : unit -> 'T) and 'T : (member Add : 'T -> ...
它开始工作了。
谁能解释一下。
【问题讨论】: