简体   繁体   中英

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

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

In your file b.ex remove the Bb 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 Bb

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

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