Oliver
How to suppress struct name expansion?
I’m generating a module from a macro, like this:
defmodule WhereTheMacroIs do
defstruct [:same_for_all]
defmacro generate(name) do
quote do
defmodule unquote(name) do
def simplified_example(%WhereTheMacroIs{} = msg), do: msg.same_for_all
end
end
end
end
defmodule WhereIUseIt do
require WhereTheMacroIs
WhereTheMacroIs.generate(SomeOtherName)
def test, do: SomeOtherName.simplified_example(%WhereTheMacroIs{same_for_all: 42})
end
The problem I get is that the struct %WhereTheMacroIs{} will be name-expanded to %WhereIUseIt.WhereTheMacroIs{} when expanding macro WhereTheMacroIs.generate/1 - which is not what I want. I want all generated modules to use the same struct internally.
I googled for a while but I was unable to find a way to suppress this extension of the struct name.
This seems to happen even if an alias is used or not.
I’m using elixir 1.6.
Most Liked
NobbZ
Oh, I do only realize now that you use the defstruct outside of the module.
So you can either use unquote(__MODULE__) (if I remember expansion rules correctly) or use Elixir.WhereTheMacroIs to specify fully qualified module name.
It wouldn’t solve your problem, yes. But I never understood the bureaucracy behind the staying outdated philosophy… Though I have to deal with it quite often due to my day work… One of our customers is still using Java5…







