F#方法重载具有byref参数

5

我正在尝试重写一个带有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

但是编译器给我返回了以下错误:

A type instantiation involves a byref type. This is not permitted by the rules of Common IL.

我不明白问题出在哪里。


1
看起来像是一个 bug;我已经提交了一个 bug 报告。 - kvb
1个回答

3
我猜这与CLI规范的第II.9.4节有关(ECMA-335),该规范指出您不能使用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

太好了。我不知道原始问题是否是编译器错误,但可能是。 - Leaf Garland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接