GithubHelp home page GithubHelp logo

elm-native-module's People

Contributors

evancz avatar gabrielperales avatar spookylukey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

elm-native-module's Issues

Add a note about functions with more than one argument

Hi Gabriel! πŸ‘‹

Thank you for putting this together: It helped me get started with Native modules in Elm 0.18! πŸ€“

NOTE: This is not really an issue, it’s more like an idea to add to the README, if it looks useful to you. πŸ˜‰

So, in my case I needed a function that’d take 2 arguments:

// src/Native/Intl.js
var _gurdiga$xo_elm$Native_Intl = function() {
  function formatMoneyAmount(localeName, amount) {
    return "yes";
  }

  return {
    formatMoneyAmount: formatMoneyAmount
  };
}();

and then I tried to call it like this:

Native.Intl.formatMoneyAmount "ro-MD" 42

It compiled, but when run in the browser (through elm-reactor) it crashed the whole Elm app with something like this in the Chrome console:😢

Main.elm:93 Uncaught TypeError: fun(...) is not a function
    at A2 (Main.elm:93)
    at _gurdiga$xo_elm$Utils_Money$format (Main.elm:13254)
    at _gurdiga$xo_elm$Dosar_Actiune_IncheiereIntentare_RezultatIncercareConciliere_PartileNuAjungLaIntelegere_BunuriUrmarite_BunUrmarit$view (Main.elm:15783)
    at itemView (Main.elm:15967)
    at Function.func (Main.elm:1390)
    at A2 (Main.elm:92)
    at Function.foldr [as func] (Main.elm:1116)
    at A3 (Main.elm:98)
    at Function.func (Main.elm:1384)
    at A2 (Main.elm:92)

Then I went on to look how it’s done in the elm-lang/core and found this in Native/Debug.js:

return {
	crash: crash,
	log: F2(log)           // ← See the F2 thingy?
};

Then I looked in my bundle and found F2:

function F2(fun)
{
  function wrapper(a) { return function(b) { return fun(a,b); }; }
  wrapper.arity = 2;
  wrapper.func = fun;
  return wrapper;
}

So my guess after all of this, is that if a native function that we want to use in Elm takes more than one argument, it has to be wrapped into an F<number> thing like that β€” it seems like further on Elm decides how many arguments to pass it. πŸ€” (I found F1 to F9 in my bundle. πŸ€“)

I’m not sure where would this insight go in the README, but I thought this may be useful for someone! πŸ€“

Cheers! πŸ™‚

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.