7stud
Where does require look for modules?
I’m trying to use a require statement:
my_lib.exs:
defmodule MyLib do
require MyMath
def go(), do: MyMath.calc(1, 2)
end
Here’s the MyMath module:
my_math.exs:
defmodule MyMath do
def calc(x, y), do: x+y
end
Both files are in the same directory. Here is what I get:
~/elixir_programs$ iex my_lib.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
** (CompileError) my_lib.exs:2: module MyMath is not loaded and could not be found
Most Liked
michalmuskala
Because macros can change semantics of the core language constructs, you have to explicitly opt-in into using them. This was a very early design decision.
mgwidmann
require does not look for files at all, the module has to be already loaded in order for it to work in the way you are trying. If you add to the top of your file Code.require_file("my_math.exs"), you should see it start working. If you do $ mix new my_project and put these files in lib (as .ex files not .exs) you won’t need to do anything and the compiler will find the right file automatically; this is the typical way to do things. After that, run $ iex -S mix and your code will be available for execution (type into the prompt MyLib.go()).
As for the code you’ve written, you don’t need a require statement at all. Requires are for loading macros before they’re used, which your function is not (its just a plain function). You can remove the require MyMath and everything will still work fine.
You can read more about alias, require, import and use here: https://elixir-lang.org/getting-started/alias-require-and-import.html
mgwidmann
Without a doubt you are causing yourself more headache than its worth. Create a new mix project and put the files in lib and call it a day. Mix makes this easy for this very reason.
michalmuskala
All require does is enables macros from that module. It doesn’t do anything else.
See the documentation: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#require/2
7stud
requiredoes not look for files at all, the module has to be already loaded
Why does elixir make you require a module before you can call the macros defined in the module? I don’t have to import a module before I call the functions defined in the module.







