jon*_*son 2 methods f# class abstract
为什么这个F#类没有编译(使用VS2010):
type Base =
abstract func : (int * int) -> int
default this.func (x : int, y : int) : int =
x + y
Run Code Online (Sandbox Code Playgroud)
func的默认实现导致此编译错误:
Error 9 This override takes a different number of arguments to the corresponding abstract member
Run Code Online (Sandbox Code Playgroud)
如果我改变它是一个成员:
type Base =
abstract func : (int * int) -> int
member this.func (x : int, y : int) : int =
x + y
Run Code Online (Sandbox Code Playgroud)
然后它编译(虽然我相信现在抽象函数缺少一个实现),并且第二个函数的类型匹配第一个.
在相关的说明中,为什么编译器不要求Base的第二个定义具有AbstractClass属性?
只是摆脱括号:
type Base =
abstract func : int * int -> int
default this.func (x : int, y : int) : int =
x + y
Run Code Online (Sandbox Code Playgroud)
你甚至可以缩短它:
default this.func(x, y) = x + y
Run Code Online (Sandbox Code Playgroud)