“基本"值只能用于直接调用被覆盖成员的基本实现 [英] 'base' values may only be used to make direct calls to the base implementations of overridden members
问题描述
为什么我不能在这里调用f
的base
实现:
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
Why can't I call the base
implementation of 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
The call to base.f
elicits this compiler error:
error FS0419: 'base' values may only be used to make direct calls to the base implementations of overridden members
If I change f
to take a single argument then it compiles. Presumably this is something to do with curried parameters vs tupled parameters, but the above code looks fine to me.
I believe that the issue is that base
can't be captured by a closure - the call has to be made directly. However, overriding a curried function automatically creates a closure since only the first argument is applied immediately. Therefore, even though it looks like you are indeed using the base
value to make a direct call to the base implementation of the overridden member, you're actually using the base
value within a closure, which is illegal.
Unfortunately, I don't think there's any great way to work around this issue. Generally, you should avoid curried members when possible, but here's one alternative:
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
这篇关于“基本"值只能用于直接调用被覆盖成员的基本实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!