alco

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.

Showing Posts 1 to 6

OvermindDL1

OvermindDL1

Instead of this, what happens if you try:

def foo(%__MODULE__.State{}), do: nil

EDIT: Actually that would not work either, what about this?

  defmacro __using__(_opts) do
    struct_name = Module.concat(__MODULE__, State)
    quote bind_quoted: [struct_name: struct_name] do
      def foo(%struct_name{}), do: nil
    end
  end

Or maybe:

  defmacro __using__(_opts) do
    struct_name = Module.concat(__MODULE__, State)
    quote bind_quoted: [struct_name: struct(struct_name)] do
      def foo(struct_name), do: nil
    end
  end

I’d have to experiment to really test, but off the top of my head maybe one of those helps you get further?

michalmuskala

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

Qqwy

TypeCheck Core Team

I think that what is going on here, is that code like this:

defmodule Foo do
  defmodule Bar do
    defstruct [:baz]
  end
end

does the following:

  • It creates the module Foo
  • It creates the the module Foo.Bar
  • It creates the the struct Foo.Bar
  • It calls alias Foo.Bar at the end of the definition of the inner module, so you can ‘just’ use it as Bar in 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

alco OP

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:

defmodule StructTestCommonUsing do
  defmacro __using__(_opts) do
    quote do
      IO.puts "Env in the quote: #{inspect __ENV__.aliases}"
      def foo(%__MODULE__.State{}), do: nil
    end
  end
end

defmodule StructTestFailingUsing do
  defmodule State do
    defstruct []
  end

  IO.puts "Env in the module: #{inspect __ENV__.aliases}"

  use StructTestCommonUsing
end

This produces the following output during compilation:

Env in the module: [{State, StructTestFailingUsing.State}]
Env in the quote: [{State, StructTestFailingUsing.State}]

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

ericmj

Elixir Core Team

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 quote docs http://elixir-lang.org/docs/master/elixir/Kernel.SpecialForms.html#quote/2.

You cannot print __ENV__.alises at 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

OvermindDL1

Ah fascinating, so my original thought was the correct way, I’ll need to remember that.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
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 Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews