7stud

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

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

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

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

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

7stud

require does 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.

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement