import Project Module
import Project.Module, only: [function: 1]
I’m sorry if the question is foolish, but I wanted to know if there is any difference.
import Project Module
import Project.Module, only: [function: 1]
I’m sorry if the question is foolish, but I wanted to know if there is any difference.
Consider this:
defmodule Project.Module
def function(arg) do
IO.inspect(arg, label: "function")
end
def another_function(arg) do
IO.inspect(arg, label: "another_function")
end
end
So then let’s use it:
defmodule User1 do
import Project.Module
def do_stuff() do
function()
another_function()
end
end
Both work because when you do import Project.Module
you include all its functions in your current scope so you don’t have to prefix them.
But if you do this:
defmodule User2 do
import Project.Module, only: [function: 1]
def do_stuff() do
function() # works.
another_function() # compile error, `another_function` not found.
Project.Module.another_function() # full scoping works.
end
end
I was actually just writing about this as I used to be confused about it.
I would also add to the answers above that all import
does(*) is allow you to call functions from other modules unqualified. The reason there is no performance hit is because the compiler effectively rewrites the calls to their fully qualified form. It doesn’t “bring in” the functions into the module like it does in other languages. ie, you can’t do this:
defmodule Bar do
def bar, do: "bar"
end
defmodule Foo do
import Bar
end
Foo.bar() # <- undefined function error
This can be quite surprising if you come from JS/Python/Ruby/Lua etc.
(*) Just to cover my bases: import
also implicitly calls require
.
I can swear the word “performance” was not in the title when I posted my answer which now looks awfully out of place, lol.
This is really surprising, the compiler doesn’t bring the all functions to the module. Thanks for your response.
Thanks for your response.