繁体   English   中英

F# function 用于 c# 库:带 byref 类型的委托

[英]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 正文中,更新了funcgrad值。 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.

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