首页 > 解决方案 > 如何将 f# fable 代码编译为多参数 javascript 函数

问题描述

嗨,我正在尝试为用 java 编写的数据库做一些基本的绑定,理想情况下我想在 F# 中实际使用它。但是我对javascript相当不熟悉。我已经设法使绑定与 1 个参数一起使用,但是由于在需要 3 个参数时尝试调用具有 1 个参数的函数,因此多个参数会使程序崩溃。

编码:

type IRNCouchDBs =
    abstract member Add : string*string*((string)->string)

[<Import("CouchDB", from = "NativeModules")>]
let couchDB : IRNCouchDBs = jsNative

在这种情况下添加是在 java 中,本质上是:

@ReactMethod 
public void Add (String name, String type, Callback cb)

如果我这样调用 f# 代码

 let str (s : string)  =  s //just a test function
couchDB.Add ("name", "type", (str "test"))

然后发生错误,抱怨我试图将 1 参数函数传递给多参数函数。

任何如何解决这个问题的想法将不胜感激。

标签: javascriptf#transpilerfable-f#

解决方案


我想通了,回想起来相当明显。

type IRNCouchDBs =
    abstract member Add : Func<string, string, ((string*string->string)), unit>

[<Import("CouchDB", from = "NativeModules")>]
let couchDB : IRNCouchDBs = jsNative


let f' = (fun (x :string,y ) -> (x + y))
couchDB.Add.Invoke (fish.FishType,fish.Name, f')

使用了 Func,并调用了它,这似乎是这样做的方法。


推荐阅读