简体   繁体   中英

Elm Native with Multiple Arity

How would one FFI into Javascript using Elm's Native modules when a term has more than one argument to a function? What is the way that elm shuffles currying around? I think I've seen some kind of projections for tuples, so I stayed away from that and didn't assume functions were uncurried, but I'm not sure now that I'm getting a type error in my function. Here's what I have for my Native/Foo.js module:

var make = function make(elm) {
    elm.Native = elm.Native || {};
    elm.Native.Foo = elm.Native.Foo || {};

    var Result = Elm.Result.make(elm);

    // Int -> Int -> Int -> Result String Date
    function someDay (year,month,day) {
        var d = new Date(year,month-1,day);
        return isNaN(d.getTime())
            ? Result.Err( 'unable to make date: '
                          + JSON.stringify({
                              "year": year,
                              "month": month,
                              "day": day
                          })
                        )
            : Result.Ok(d)
    }

    if (elm.Native.Foo.values) return elm.Native.Foo.values;

    return elm.Native.Foo.values = {
        'someDay': someDay
    };
};

Elm.Native.Foo = {};
Elm.Native.Foo.make = make;

and in my module that I'm trying to use it, I'm assuming it's already curried:

module Foo where

import Native.Foo

import Date exposing (Date, Month)
import Date.Extra.Core exposing (monthToInt)    

someDay : Int -> Month -> Int -> Date
someDay y m d =
  case Native.Today.someDay y (monthToInt m) d of
    Ok d -> d
    Err e -> Debug.crash e

It compiles just fine, but when I try to run my app, I'm getting the following error:

TypeError: fun(...) is not a function

Is there anything I'm doing wrong here?

To curry raw javascript functions, use the F2 , F3 ... functions. They're in global scope by default, so to fix this just do this:

// ...
    return elm.Native.Foo.values = {
        'someDay': F3(someDay)
    };

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