mguimas
Import and macro conflict
Hello.
Suppose we have a file with this code
import ImportMe
defmodule AModule do
use UseMe
end
where the modules above are defined in another file like this
defmodule ImportMe do
def bar, do: :ok
end
defmodule UseMe do
defmacro __using__(_opts) do
quote do
def foo, do: bar()
def bar(), do: :ok
end
end
end
The compilation of these two files produces the imported ImportMe.bar/0 conflicts with local function error in the first file.
Is this something that should be reported as a bug or not?
Thanks
Most Liked
LostKobrakai
Why would it be a bug? You clearly have two functions named bar available in the same scope. What if you change the order of your functions to:
def bar(), do: :ok
def foo, do: bar()
What should is call in that case? It’s not clear.
LostKobrakai
I’m not really sure how this is related to macros at all. You have two different functions named the same in the same scope. That’s not going to work well. The compiler cannot decide for you which of the two ones it’s supposed to call. How those named functions ended up in the same scope shouldn’t make any difference.
NobbZ
When you are in AModule, there is no difference between bar and AModule.bar.
Just cope with it. Calling a macro is the same as copy and pasting code into your file. If you do not want a function f generated, then do not call the macro.
If you do not want to import a function g, then :exclude it from the import list.
That aside, I have a very strict opinion about libraries narrowing my own available namespace by dumping arbitrary functions into my module. But I think we already argued about this a lot in the slack…
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








