繁体   English   中英

带有byref参数覆盖的F#方法

[英]F# method with byref parameter override

我试图用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.

我不明白是什么问题

我想这与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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM