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…
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








