如何使用 from Fable 定义和调用多参数函数
How to define and call a multi argument function with from Fable
我正在尝试使用 2 个参数调用 Redux.createStore,以下代码简化了签名,但我无法让 FSharp 理解我想要的内容:
[<Import("*","Redux")>]
module redux =
let createStore: (System.Func<int,int> -> int) = jsNative
let store = redux.createStore 22//no idea what to do here
所以假设我想将 createStore 定义为一个接受 2 个整数的函数,而不是一个包含 2 个整数的元组,而不是一个接受一个 int 的函数和 returns 一个接受一个 int 的函数(部分适用2 个整数)。不,它是一个带有 2 个参数(比如整数)和 returns 一个整数的本机函数。
在示例中有一个 redux 示例,它只使用一个参数。
documentation 展示了如何创建一个带多个参数的函数,但没有展示如何定义类型,也没有展示如何调用这样的函数。
该示例确实显示了具有多个参数的定义,但从未调用过它,因此仍然不知道如何调用带有多个参数的 js 函数。
根据您的评论:
I'm trying to figure out how to define a js function that takes 2 parameters and call it with 2 parameters.
答案是,照你说的去做:
[<Import("*","Redux")>]
module redux =
let createStore (a: int, b: int): int = jsNative
let store = redux.createStore (22, 42)
除了代码示例,我无法添加任何其他内容,因为我不确定您的实际问题是什么:您是否发现了一些关于此(什么?)的 non-obvious,如果您尝试了这个,但不知何故(如何?)或其他原因(什么?)不起作用。
真的厌倦了尝试定义 F# 和 Fable 理解的类型,然后尝试调用它。
现在我创建了一个名为 JSI.js 的 JavaScript 文件:
/**
* JavaScipt Interop to call complex JavaScipt library methods
* where I have no clue how to define the type for in F#/Fable
*/
define(["exports","redux"],function(exports,Redux){
exports.createStore =
(fn)=>
(initialStore)=>{
return Redux.createStore(
(action,state)=>fn(action)(state)
,initialStore
);
}
});
然后在main.fsx:
[<Import("*","../js/JSI")>]
module JSI =
let createStore:
(
(AppController.ApplicationModel -> Action -> AppController.ApplicationModel)
-> AppController.ApplicationModel
-> int
) = jsNative
let store =
JSI.createStore
applicationHandler
AppController.defaultModel
这将 wrap/unwrap F# 将函数部分应用到具有多个参数的对象,供 Redux 使用。
我正在尝试使用 2 个参数调用 Redux.createStore,以下代码简化了签名,但我无法让 FSharp 理解我想要的内容:
[<Import("*","Redux")>]
module redux =
let createStore: (System.Func<int,int> -> int) = jsNative
let store = redux.createStore 22//no idea what to do here
所以假设我想将 createStore 定义为一个接受 2 个整数的函数,而不是一个包含 2 个整数的元组,而不是一个接受一个 int 的函数和 returns 一个接受一个 int 的函数(部分适用2 个整数)。不,它是一个带有 2 个参数(比如整数)和 returns 一个整数的本机函数。
在示例中有一个 redux 示例,它只使用一个参数。
documentation 展示了如何创建一个带多个参数的函数,但没有展示如何定义类型,也没有展示如何调用这样的函数。
该示例确实显示了具有多个参数的定义,但从未调用过它,因此仍然不知道如何调用带有多个参数的 js 函数。
根据您的评论:
I'm trying to figure out how to define a js function that takes 2 parameters and call it with 2 parameters.
答案是,照你说的去做:
[<Import("*","Redux")>]
module redux =
let createStore (a: int, b: int): int = jsNative
let store = redux.createStore (22, 42)
除了代码示例,我无法添加任何其他内容,因为我不确定您的实际问题是什么:您是否发现了一些关于此(什么?)的 non-obvious,如果您尝试了这个,但不知何故(如何?)或其他原因(什么?)不起作用。
真的厌倦了尝试定义 F# 和 Fable 理解的类型,然后尝试调用它。
现在我创建了一个名为 JSI.js 的 JavaScript 文件:
/**
* JavaScipt Interop to call complex JavaScipt library methods
* where I have no clue how to define the type for in F#/Fable
*/
define(["exports","redux"],function(exports,Redux){
exports.createStore =
(fn)=>
(initialStore)=>{
return Redux.createStore(
(action,state)=>fn(action)(state)
,initialStore
);
}
});
然后在main.fsx:
[<Import("*","../js/JSI")>]
module JSI =
let createStore:
(
(AppController.ApplicationModel -> Action -> AppController.ApplicationModel)
-> AppController.ApplicationModel
-> int
) = jsNative
let store =
JSI.createStore
applicationHandler
AppController.defaultModel
这将 wrap/unwrap F# 将函数部分应用到具有多个参数的对象,供 Redux 使用。