[英]F# function to be used in c# library : delegate with byref type
[英]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.