2016-05-04 8 views
2

Wie würde ein FFI in Javascript mit Elms Native Module, wenn ein Begriff mehr als ein Argument für eine Funktion hat? Wie schlurft die Ulme herum? Ich denke, ich habe eine Art von Projektionen für Tupel gesehen, also bin ich davon weggeblieben und habe nicht angenommen, dass die Funktionen uncurried waren, aber ich bin mir nicht sicher, ob ich jetzt einen Typfehler in meiner Funktion bekomme. Hier ist, was ich für meine Native/Foo.js Modul:Elm Native mit Multiple Arity

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; 

und in meinem Modul, das ich versuche, es zu benutzen, ich nehme an, es ist schon 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 

Es kompiliert ganz gut, aber wenn ich versuche, meine app zu laufen, erhalte ich folgende Fehlermeldung:

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

gibt es etwas, ich bin falsch hier?

Antwort

2

Um rov Javascript-Funktionen zu curry, verwenden Sie die F2, F3 ... Funktionen. Sie sind standardmäßig im globalen Gültigkeitsbereich. Um dies zu beheben, gehen Sie einfach wie folgt vor:

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