7stud

7stud

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

Showing Posts 1 to 10

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

Qqwy

Qqwy

TypeCheck Core Team

Related to this: A file containing a module with a name like Foo.Bar.Baz is placed in foo/bar/ and named baz.ex by convention, but there is nothing stopping you from doing it differently (except the very strong argument of confused coworkers).

Sometimes there are reasons for not following this convention though, for instance when having (for programmatic reasons) to define multiple modules in the same file.

gregvaughn

gregvaughn

You should be using *.ex files not *.exs files. The latter are only compiled in memory and beam files are not written to disk, which is why MyMath cannot be found. For a minimal fix, just rename my_math.exs to my_math.ex

7stud

7stud OP

Doesn’t work:

~/elixir_programs$ ls -al my_lib.exs my_math.ex 
-rw-r--r--  1 7stud  staff  77 Aug 15 21:48 my_lib.exs
-rw-r--r--  1 7stud  staff  50 Aug 15 21:48 my_math.ex

~/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


~/elixir_programs$ cat my_lib.exs 
defmodule MyLib do
  require MyMath
  
  def go(), do: MyMath.calc(1, 2)
end

~/elixir_programs$ cat my_math.ex
defmodule MyMath do
  def calc(x, y), do: x+y
end
gregvaughn

gregvaughn

It’s not clear from what you’ve shown whether you’re in a mix project and whether you’ve compiled my_math.ex to generate a beam file. If you’re not using mix, then you need to compile by hand.

$ elixirc my_math.ex

will generate a file in the current directory called Elixir.MyMath.beam. Now that you have a beam file, it can be called in your exs script.

7stud

7stud OP

I’m not using a mix project.

If you’re not using mix, then you need to compile by hand.

$ elixirc my_math.ex

Okay, that worked…but it also worked without the require statement, so I guess I don’t understand what require does yet:

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.

The problem is that the book I’m reading hasn’t covered macros yet, but in the code examples there are lines with require, use, and import statements.

gregvaughn

gregvaughn

require in Elixir is not like require in your prior language. That quote you included from @mgwidmann explains it well. require is about macros, and you’re not using macros.

Edit to add:
The getting started guide is a good place to … well .. get started. It covers require in more depth here: https://elixir-lang.org/getting-started/alias-require-and-import.html#require

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: Kernel.SpecialForms — Elixir v1.20.2

7stud

7stud OP

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? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews