为什么我不能打电话base
实施f
here:
type Base =
abstract f : int -> int -> int
default this.f (x : int) (y : int) : int = x + y
type Derived =
inherit Base
override this.f (x : int) (y : int) : int = base.f -x -y
致电给base.f
引发此编译器错误:
error FS0419: 'base' values may only be used to make direct calls to the base implementations of overridden members
如果我改变f
接受一个参数然后编译。据推测,这与柯里化参数与元组参数有关,但上面的代码对我来说看起来不错。
我相信问题是base
无法通过闭包捕获 - 必须直接进行调用。但是,重写柯里化函数会自动创建一个闭包,因为只有第一个参数会立即应用。因此,即使看起来您确实在使用base
值来直接调用被重写成员的基本实现,您实际上正在使用base
闭包内的值,这是非法的。
不幸的是,我认为没有任何好的方法可以解决这个问题。一般来说,您应该尽可能避免使用柯里化成员,但这里有一种选择:
type Base =
abstract f : int -> (int -> int)
default this.f (x : int) = fun y -> x + y
type Derived =
inherit Base
override this.f x =
let fn = base.f -x
fun y -> fn -y
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)