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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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

We're in Beta

About us Mission Statement