7rans
Import in an import, functions not found?
I have a helper module for importing that I want to split into two modules and import the first into the second (so I can reuse the first). Basically:
defmodule DataHelpers do
def foo(), do: ...
end
defmodule ViewHelpers do
import DataHelpers
end
defmodule SomeOtherModule do
import ViewHelpers
end
I get an error that foo() is not defined in SomeOtherModule.
Most Liked
benwilson512
Hi @7rans, I think you’re confused about what import does. When you import DataHelpers inside of ViewHelpers you allow code inside of ViewHelpers to refer to functions inside DataHelpers to call functions from DataHelpers without specifying the module. What it does not do is copy or add those functions into DataHelpers.
If you want both of those helpers in SomeOtherModule you’ll need to import both modules.
lud
You could do this:
defmodule DataHelpers do
def foo() do
# ...
end
end
defmodule ViewHelpers do
defmacro __using__(_) do
quote do
import DataHelpers
end
end
end
defmodule SomeOtherModule do
use ViewHelpers
end
But it is only interesting if useing ViewHelpers will import multiple modules, otherwise it is just useless indirection.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









