F # method with byref overriding

I am trying to override a method with the byref parameter, below is a sample code

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

but the compiler will give me this error:

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

I do not understand what the problem is

+4
source share
1 answer

I assume this is due to section II.9.4 of the CLI specification ( ECMA-335 ), which states that you cannot create typical types with parameters byref.

But where is the type type instance? In repeating, I think this may be due to the signature of the abstract method Incrementin which there is a int byref * int byreftuple. But I would not expect a tuple to be created when the method is called.

, -, 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
+3

Source: https://habr.com/ru/post/1545348/


All Articles