Hi all
I am trying to compile my exs file and I’ve got
module MyMacro is not loaded and could not be found
and do not know, what am I doing wrong.
I have two modules, the one import the another, that looks like(the filename is my_macro.exs):
defmodule MyMacro do
defmacro example({value, _, _}) do
IO.puts "You'll see me at compile-time: #{inspect value}"
quote do
IO.puts "You'll see me at run-time: #{inspect unquote(value)}"
end
end
end
and the other(the filename is run.exs):
defmodule Run do
import MyMacro
def run do
IO.puts example(2 + 3)
end
end
When I try to compile the run.exs file, I’ve got error: iex(2)> c "run.exs"
== Compilation error on file run.exs ==
** (CompileError) run.exs:5: module MyMacro is not loaded and could not be found
I don’t know what you did before compiling run.exs, but probably you missed to compile my_macro.exs, also it maybe, that you need to load the module after compiling.
NB: You are not meant to compile *.exs but *.ex. Even if this distinction is merely theorethically today, it is still an important sign for developers, if the can “run” a file or if they need to compile it.
As I said already, you need to compile the file containing MyMacro before you use its Macros in another module.
Proof:
$ ls *.ex
my_macro.ex run.ex
$ ls *.beam
zsh: no matches found: *.beam
# my_macro.ex
defmodule MyMacro do
defmacro example({value, _, _}) do
IO.puts "You'll see me at compile-time: #{inspect value}"
quote do
IO.puts "You'll see me at run-time: #{inspect unquote(value)}"
end
end
end
# run.ex
defmodule Run do
import MyMacro
def run do
IO.puts example(2+3)
end
end
$ iex
Erlang/OTP 18 [erts-7.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.2.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "run.ex"
== Compilation error on file run.ex ==
** (CompileError) run.ex:2: module MyMacro is not loaded and could not be found
** (exit) shutdown: 1
(elixir) lib/kernel/parallel_compiler.ex:202: Kernel.ParallelCompiler.handle_failure/5
(elixir) lib/kernel/parallel_compiler.ex:185: Kernel.ParallelCompiler.wait_for_messages/8
(elixir) lib/kernel/parallel_compiler.ex:55: Kernel.ParallelCompiler.spawn_compilers/3
(iex) lib/iex/helpers.ex:168: IEx.Helpers.c/2
iex(1)> c "my_macro.ex"
[MyMacro]
iex(2)> c "run.ex"
You'll see me at compile-time: :+
[Run]
iex(3)> Run.run
You'll see me at run-time: :+
ok
:ok
iex(4)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
^C%
$ ls *.beam
Elixir.MyMacro.beam Elixir.Run.beam