[英]F# function to be used in c# library : delegate with byref type
我有一個 c# dll 庫,其中一種方法g
接受以下 function f
in ZD7EFA19FBE742972FDZ5ADB06 作為輸入
void f(double[] x, ref double func, double[] grad, object obj)
在 function 正文中,更新了func
和grad
值。 F# 中g(f)
的輸入簽名為
f:float[]-> byref<float> -> float[]-> obj->unit
如何在 f# 中寫入f
以在 f# 中使用g(f)
?
編輯當我按照下面ildjarn的回答時
let f(x:float[]) (func:byref<float>) (grad:float[]) (obj: obj) =
func <- ...
grad.[0] <- ...
grad.[1] <- ...
...
()
我在g(f)
上遇到錯誤
此 function 值用於構造其簽名包含 byref 參數的委托類型。 您必須使用顯式 lambda 表達式,采用 4 arguments。
我應該怎么做才能讓它工作? 謝謝。 更新:事實證明我必須使用g(fun (x:float[]) (func:byref<float>) (grad:float[]) (obj: obj) ->...
。不能給出內部 function像f
這樣的名字。
let function1 (x:double[], func:byref<double>, grad:double[], obj:obj) = ...
A canonical way to expose F# functions to other .NET language is to use a class, sometimes a static class, eg the design of the LChart library ( link ):
Excel .NET的財務函數是用F#編寫的項目,是一個通用的Z303CB0EF9EDB9082D61BBBE5825庫。 可以看到它的public API是怎么寫的。 您還可以了解如何將 FSharp.Core.dll 鏈接到 dll 以便目標機器不需要安裝 F# 運行時。
更新:
ref double func
被轉換為 f#,因為let y = ref (func)
, double[] grad
在 F# 中只是float[]
。 請參閱以下示例以獲取ref
:
public class tt
{
public static void temp(int x, ref double[] y)
{
y = new double[10];
y[1] = 10;
}
}
在 F# 中:
let y = ref (Array.create 10 0.0)
tt.temp(0, y)
printfn "%A" y
0
注意temp
的第二個參數是一個ref
類型。 您只需使用 F# 中的ref
運算符為數組 object 創建一個 ref 句柄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.