Filipewelton

Filipewelton

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.

Showing Posts 1 to 6

dimitarvp

dimitarvp

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

Link to docs.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @Filipewelton there are no differences in either compile time or runtime performance.

sodapopcan

sodapopcan

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.

dimitarvp

dimitarvp

I can swear the word “performance” was not in the title when I posted my answer which now looks awfully out of place, lol.

Filipewelton

Filipewelton OP

This is really surprising, the compiler doesn’t bring the all functions to the module. Thanks for your response.

Filipewelton

Filipewelton OP

Thanks for your response.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews