简体   繁体   中英

Using Elixir module from Erlang fails

I am trying to use in Erlang module a beam-file compiled from Elixir source. It raises error when I run Erlang node but I can use the code directly from Elixir.

Elixir module:

defmodule Recursion do
  def print_multiple_times(msg, n) when n <= 1 do
    IO.puts msg
  end

  def print_multiple_times(msg, n) do
    IO.puts msg
    print_multiple_times(msg, n - 1)
  end
end

Erlang module:

-module(use_recur).
-define(elixir__recursion, 'Elixir.Recursion').

-export([hey/0]).

hey() ->
    ?elixir__recursion:print_multiple_times("Hello!", 3).

Compile both:

$ rm -f *.beam $ elixirc recursion.ex
$ erlc use_recur.erl

Run Erlang:

$ erl -run use_recur hey -run init stop -noshell {"init terminating in do_boot",{undef,[{'Elixir.IO',puts,["Hello!"],[]},{'Elixir.Recursion',print_multiple_times,2,[{file,"recursion.ex"},{line,7}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}} init terminating in do_boot ({undef,[{Elixir.IO,puts,Hello!,[]},{Elixir.Recursion,print_multiple_times,2,[{ },{ }]},{init,start_em,1,[]},{init,do_boot,3,[]}]})

Crash dump is being written to: erl_crash.dump...done

Elixir script:

Recursion.print_multiple_times "Hello!", 3

Runs successfully:

$ elixir elx_recur.exs  
Hello!
Hello!
Hello!

Why does it happen? I'd say Erlang's output should be the same.

The error means that Erlang could not find a module named 'Elixir.IO' . This module is part of core Elixir. You'll need to add the ebin folder of your Elixir installation to Erlang's load path using -pa (or other similar flags like -pz ) to make Erlang load Elixir's core libraries, as that folder contains the compiled .beam files of Elixir core, including Elixir.IO.beam .

erl -pa /path/to/elixir/ebin ...

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