简体   繁体   中英

How to define and call a multi argument function with from Fable

I'm trying to call Redux.createStore with 2 arguments, the following code simplifies the signature but I am unable to get FSharp to understand what I want:

[<Import("*","Redux")>]
module redux =
    let createStore: (System.Func<int,int> -> int) = jsNative

let store = redux.createStore 22//no idea what to do here

So lets say I want to define createStore as a function that takes 2 ints, not a tuple of 2 ints and not a function that takes one int and returns a function that takes one int (partially apply 2 ints). No, it is a native function that takes 2 arguments (say ints) and returns an int.

In the examples there is a redux example that is only ever used taking one parameter.

The documentation shows how to create a function taking multiple arguments but not how to define the type nor how to call such a function.

The example does show the definition with multiple arguments but never calls it so still don't know how to call a js function that takes multiple arguments.

Based on your comment:

I'm trying to figure out how to define a js function that takes 2 parameters and call it with 2 parameters.

The answer is, just do exactly what you said:

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

let store = redux.createStore (22, 42)

Except for the code example, I can't add anything else, because I'm not sure what your actual question was: whether you found something non-obvious about this (what?), of if you tried this and it didn't work somehow (how?), or something else (what?).

Getting really tired of trying to define a type that F# and Fable understand and then trying to call it.

For now I created a JavaScript file called JSI.js:

/**
 * 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
      );
    }
});

Then in 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

That would wrap/unwrap F# partially applied functions to something with multiple arguments to be used by Redux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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