繁体   English   中英

如何从寓言中定义和调用多参数函数

[英]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并返回接受一个int的函数的函数(部分应用2个int)。 不,这是一个带有2个参数(例如int)并返回int的本机函数。

在示例中,有一个redux示例,该示例仅使用一个参数。

文档显示了如何创建带有多个参数的函数,但不介绍如何定义类型或如何调用此类函数。

该示例确实显示了具有多个参数的定义,但从未调用过它,因此仍然不知道如何调用带有多个参数的js函数。

根据您的评论:

我试图弄清楚如何定义一个接受2个参数并用2个参数调用它的js函数。

答案是,完全按照您说的做:

[<Import("*","Redux")>]
module redux =
    let createStore (a: int, b: int): int = jsNative

let store = redux.createStore (22, 42)

除了代码示例外,我无法添加其他任何内容,因为我不确定您的实际问题是什么:您是否发现了一些不明显的内容(是什么?),是否尝试过但没有发现?以某种方式(如何?)或其他方式(什么?)工作。

尝试定义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

这会将F#部分应用的函数包装/解包到Redux要使用的具有多个参数的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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