Cae*_*lan 5 .net parameters methods f# byref
我试图用 byref 参数覆盖一个方法,下面的代码是一个例子
type Incrementor(z) =
abstract member Increment : int byref * int byref -> unit
default this.Increment(i : int byref,j : int byref) =
i <- i + z
type Decrementor(z) =
inherit Incrementor(z)
override this.Increment(i : int byref,j : int byref) =
base.Increment(ref i,ref j)
i <- i - z
Run Code Online (Sandbox Code Playgroud)
但是编译器给了我这个错误:
A type instantiation involves a byref type. This is not permitted by the rules of Common IL.
Run Code Online (Sandbox Code Playgroud)
我不明白有什么问题
我猜想这与 CLI 规范 ( ECMA-335 )的 II.9.4 节有关,该节规定不能使用byref
参数实例化泛型类型。
但是泛型类型实例化在哪里呢?再次猜测,我认为这可能与抽象方法的签名有关Increment
,其中包含int byref * int byref
元组。但是,我没想到在调用方法时会创建一个元组。
实际的问题似乎只是由base.Increment(ref i, ref j)
调用触发的,如果删除它然后它会编译。如果您删除 byref 参数之一,例如 ,它也会编译abstract member Increment : int byref -> unit
。
您可以改用显式ref
类型,但从您的示例中不清楚您要做什么。
type Incrementor(z) =
abstract member Increment : int ref * int ref -> unit
default this.Increment(i: int ref, j: int ref) =
i := !i + z
type Decrementor(z) =
inherit Incrementor(z)
override this.Increment(i: int ref, j: int ref) =
base.Increment(i, j)
i := !i - z
Run Code Online (Sandbox Code Playgroud)