luamoonscriptlua-loadfile

How to call a function from moonscript in lua?


I've got a moon script code like this:

hello = (name) ->
  print "Hello #{name}!"

And i want to use it in my lua code using moonscript.loadfile

how should i do something like that?


Solution

  • MoonScript code compiles to Lua, so the function you've written is actually a Lua function when it's being executed.

    There are a few ways to get access to it in Lua:

    Keep in mind that if you have function in another file, you need to export them as part of the module. You do this by having a return value for the module. The typically pattern is to return a table that contains all the function you would want to use. In MoonScript, the last line in a file is automatically converted into a return statement. Assignment is no coerced into a return though, so I recommend structuring your module like this:

    hello = (name) ->
      print "Hello #{name}!"
    
    {:hello}