Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a module into another module using Elixir language?

Tags:

elixir

I'm having hard time to load a module into another module using Elixir language.

For example, I have 2 files shown below:

a.ex

defmodule A do
  def a do
    IO.puts "A.a"
  end
end

b.ex

defmodule B do
  require A

  def b do
    IO.puts "B.b"
    A.a
  end
end

B.b

I tried to execute b.ex. Then I got error shown below:

$elixir b.ex
** (CompileError) b.ex:2: module A is not loaded and could not be found
like image 850
Hideshi Ogoshi Avatar asked Dec 06 '15 03:12

Hideshi Ogoshi


1 Answers

In your file b.ex remove the B.b from the last line

Then in your project directory run Iex like so

iex -S mix

This will load iex and load your modules correcly

Then you can just do B.b

and you'll see:

B.b
A.a
:ok

Also, make sure your a.ex and b.ex files are in the lib/ directory of your elixir project

like image 53
TheStoneFox Avatar answered Oct 24 '22 00:10

TheStoneFox