alco
Hey folks.
I’ve got a simple use case for macros based on the following example module:
defmodule StructTestWorking do
defmodule State do
defstruct []
end
def foo(%State{}), do: nil
end
I want to extract the function foo into a “template module” and then use that module:
defmodule StructTestCommonUsing do
defmacro __using__(_opts) do
quote do
def foo(%State{}), do: nil
end
end
end
defmodule StructTestFailingUsing do
defmodule State do
defstruct []
end
use StructTestCommonUsing
end
As the module name suggests, this doesn’t work. I’m getting this compilation error:
== Compilation error on file lib/struct_test_failing_using.ex ==
** (CompileError) lib/struct_test_failing_using.ex:14: State.__struct__/0 is undefined, cannot expand struct State
(stdlib) lists.erl:1353: :lists.mapfoldl/3
It doesn’t work when using a @before_compile hook either:
defmodule StructTestCommonBeforeCompile do
defmacro __using__(_opts) do
quote do
@before_compile unquote(__MODULE__)
end
end
defmacro __before_compile__(_env) do
quote do
def foo(%State{}), do: nil
end
end
end
defmodule StructTestFailingBeforeCompile do
defmodule State do
defstruct []
end
use StructTestCommonBeforeCompile
end
This is using Elixir v1.3.1. Would you expect this to work?
I’ve also pushed the code to a repo – https://github.com/alco/struct_test.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Instead of this, what happens if you try:
EDIT: Actually that would not work either, what about this?
Or maybe:
I’d have to experiment to really test, but off the top of my head maybe one of those helps you get further?
michalmuskala
Maybe this issue might be related: Trouble defining struct in dynamically named module · Issue #4894 · elixir-lang/elixir · GitHub
Have you tried with Elixir master?
Qqwy
I think that what is going on here, is that code like this:
does the following:
FooFoo.BarFoo.Baralias Foo.Barat the end of the definition of the inner module, so you can ‘just’ use it asBarin the outside module.This alias is something that does not happen when you extract the functionality elsewhere.
What you might want to do instead, inside your
__using__definition, is to call the State struct as%__MODULE__.State{}, which will add the outer wrapping module to the struct name.alco
Using
%__MODULE__.State{}does indeed work. What I find confusing is that the aliases in__ENV__are the same both inside the quoted block and in the module definition itself:This produces the following output during compilation:
Apparently there’s some other compilation context that is not getting populated with the alias.
@michalmuskala I’ve tried Elixir master, it doesn’t make a difference.
ericmj
Aliases uses the environment outside of the quote. You don’t want alises in the environment where you inject code to affect the code in your quote. This is for the same reasons we have variable hygiene - you don’t want variables from another context to affect variables in the current context.
Check out the aliases hygiene section in the
quotedocs http://elixir-lang.org/docs/master/elixir/Kernel.SpecialForms.html#quote/2.You cannot print
__ENV__.alisesat runtime like that to get the environment of the code at compile time.__MODULE__works the same way when you think about it, it returns the module where the code is injected, so__ENV__will also return the environment where the code is injected.OvermindDL1
Ah fascinating, so my original thought was the correct way, I’ll need to remember that.