With background from Python/Java/Golang languages, the import
vs use
was also confused for me. This will explain code reuse mechanism with some declarative languages examples.
import
In short, in Elixir, you don't need to import modules. All public functions can be accessed by full-qualified MODULE.FUNCTION syntax:
iex()> Integer.mod(5, 2)
1
iex()> String.trim(" Hello Elixir ")
"Hello Elixir"
In Python/Java/Golang, you need to import MODULE
before you can use functions in that MODULE, e.g Python
In []: import math
In []: math.sqrt(100)
Out[]: 10.0
Then what import
in Elixir does might surprise you:
We use import whenever we want to easily access functions or macros from other modules without using the fully-qualified name
https://elixir-lang.org/getting-started/alias-require-and-import.html#import
So if you want to type sqrt
instead of Integer.sqrt
, trim
instead of String.trim
, import
will help
iex()> import Integer
Integer
iex()> sqrt(100)
10.0
iex()> import String
String
iex()> trim(" Hello Elixir ")
"Hello Elixir"
This might cause problems for reading code and when there is name-conflicting so it is not recommended in Erlang (the language that influences Elixir). But there is no such convention in Elixir, you can use it at own-risk.
In Python, the same effect can be done by:
from math import *
and it only recommended to use in some special scenarios / interactive mode - for shorter/faster typing.
use & require
What makes use
/require
different is that they relate to "macro" - the concept that does not exist in Python/Java/Golang... family.
You don't need to import
a module to use its functions, but you need to require
a module to use its macros:
iex()> Integer.mod(5, 3) # mod is a function
2
iex()> Integer.is_even(42)
** (CompileError) iex:3: you must require Integer before invoking the macro Integer.is_even/1
(elixir) src/elixir_dispatch.erl:97: :elixir_dispatch.dispatch_require/6
iex()> require Integer
Integer
iex()> Integer.is_even(42) # is_even is a macro
true
Though is_even
can be written as a normal function, it is a macro because:
In Elixir, Integer.is_odd/1 is defined as a macro so that it can be used as a guard.
https://elixir-lang.org/getting-started/alias-require-and-import.html#require
use
, to excerpt from Elixir doc:
use requires the given module and then calls the __using__/1
callback on it allowing the module to inject some code into the current context.
defmodule Example do
use Feature, option: :value
end
is compiled into
defmodule Example do
require Feature
Feature.__using__(option: :value)
end
https://elixir-lang.org/getting-started/alias-require-and-import.html#use
So writing use X
is same as writing
require X
X.__using__()
use/2
is a macro, macro will transform code into other code for you.
You will want to use MODULE
when you:
- want to access its macros (
require
)
- AND execute
MODULE.__using__()
Tested on Elixir 1.5
import Module
brings in functions to be used inside your module.use Module
brings in functions to be used AND exposes them publicly on your module – Jered