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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mgwidmann
requiredoes 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 fileCode.require_file("my_math.exs"), you should see it start working. If you do$ mix new my_projectand put these files inlib(as.exfiles 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 mixand your code will be available for execution (type into the promptMyLib.go()).As for the code you’ve written, you don’t need a
requirestatement at all. Requires are for loading macros before they’re used, which your function is not (its just a plain function). You can remove therequire MyMathand everything will still work fine.You can read more about
alias,require,importandusehere: https://elixir-lang.org/getting-started/alias-require-and-import.htmlQqwy
Related to this: A file containing a module with a name like
Foo.Bar.Bazis placed infoo/bar/and namedbaz.exby 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
You should be using
*.exfiles not*.exsfiles. The latter are only compiled in memory and beam files are not written to disk, which is whyMyMathcannot be found. For a minimal fix, just renamemy_math.exstomy_math.ex7stud
Doesn’t work:
gregvaughn
It’s not clear from what you’ve shown whether you’re in a mix project and whether you’ve compiled
my_math.exto generate a beam file. If you’re not using mix, then you need to compile by hand.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
I’m not using a mix project.
Okay, that worked…but it also worked without the require statement, so I guess I don’t understand what require does yet:
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
requirein Elixir is not likerequirein your prior language. That quote you included from @mgwidmann explains it well.requireis 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
requirein more depth here: https://elixir-lang.org/getting-started/alias-require-and-import.html#requiremgwidmann
Without a doubt you are causing yourself more headache than its worth. Create a new mix project and put the files in
liband call it a day. Mix makes this easy for this very reason.michalmuskala
All
requiredoes is enables macros from that module. It doesn’t do anything else.See the documentation: Kernel.SpecialForms — Elixir v1.20.2
7stud
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.