首页 文章

如何使用Fable定义和调用多参数函数

提问于
浏览
0

我试图用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个整数) . 不,它是一个本机函数,它接受2个参数(比如整数)并返回一个int .

在示例中,有一个redux示例,只使用一个参数 .

documentation显示了如何创建一个接受多个参数的函数,但不知道如何定义类型以及如何调用这样的函数 .

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

2 回答

  • 0

    根据您的评论:

    我试图弄清楚如何定义一个带有2个参数的js函数,并用2个参数调用它 .

    答案是,就像你说的那样:

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

    除了代码示例,我不能添加任何其他东西,因为我不确定你的实际问题是什么:你是否发现了一些不明显的东西(什么?),如果你尝试了这个并且没有以某种方式工作(如何?),或其他什么(什么?) .

  • 2

    真的厌倦了尝试定义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使用的多个参数的东西 .

相关问题