How to load a module into another module using Elixir?

I find it difficult to load a module into another module using the Elixir language.

For example, I have 2 files shown below:

a.ex

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

b.ex

 defmodule B do require A def b do IO.puts "Bb" Aa end end Bb 

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

 $elixir b.ex ** (CompileError) b.ex:2: module A is not loaded and could not be found 
+5
source share
1 answer

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

Then in your project directory, run Iex like this

 iex -S mix 

This will load iex and load your modules correctly

Then you can just do Bb

and you will see:

 Bb Aa :ok 

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

+4
source

Source: https://habr.com/ru/post/1237481/


All Articles