beno

beno

Warning about unused import & undefined/private function error

The code below throws the error below, and I did not expect that, so I was wondering if this is intended behaviour. If so, how would one define a code structure like this? The idea is to have a module Foo that holds some shared functions, imported into several modules similar to Bar, which are then called from yet another module Baz.

defmodule Foo do
  def foo do
    "bar"
  end
end

defmodule Bar do
  import Foo
end

defmodule Baz do
  def baz do
    Bar.foo()
  end
end

Baz.baz()

$ warning: unused import Foo
$ ** (UndefinedFunctionError) undefined function: Bar.foo/0

Most Liked

OvermindDL1

OvermindDL1

Actually, look at defdelegate:

defmodule Foo do
  def foo do
    "bar"
  end
end

defmodule Bar do
  defdelegate foo, to: Foo
end

defmodule Baz do
  def baz do
    Bar.foo()
  end
end
wojtekmach

wojtekmach

Hex Core Team

Hi Beno,
This is expected behavior. Per h import:

import/2 allows one to easily access functions or macros from others modules
without using the qualified name.

so it’s making the imported functions available to easily call within your module, nothing more. Thus, in your use case, the following is probably what you wanted.

defmodule Baz do
  import Foo

  def baz do
    foo
  end
end

If you find yourself wanting to import certain functions a lot you can use the use macro like this:

defmodule Shared do
  defmacro __using__(_opts) do
    quote do
      import Foo
    end
  end
end

defmodule Baz do
  use Shared

  def baz do
    foo
  end
end

see h use for some more docs around this feature.

beno

beno

You’re the best. Thanks!

Where Next?

Popular in Questions Top

New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement