Oliver
Macro does not like module attribute?
In file 1:
defmodule Utils.Struct do
# this is for importing definitions when "use Utils.Struct" is called
defmacro __using__(_opts) do
quote do
import Utils.Struct
end
end
defmacro defstruct_module(struct_name, fields) do
quote do
defmodule unquote(struct_name) do
use Utils.Access
defstruct unquote(fields)
end
end
end
end
In file 2:
defmodule Statistical.Generator do
use Utils.Struct
@in_files [:in_cause, :out_cause, :start]
defstruct_module(State, @in_files)
Breaks:
warning: undefined module attribute @in_files, please remove access to @in_files or explicitly set it before access
<some file>: Statistical.Generator.State (module)
== Compilation error in file <some file> ==
** (ArgumentError) struct fields definition must be list, got: nil
(elixir 1.14.4) lib/kernel/utils.ex:117: Kernel.Utils.defstruct/3
But this works - again file 2:
defmodule Statistical.Generator do
use Utils.Struct
@in_files [:in_cause, :out_cause, :start]
in_files = @in_files
defstruct_module(State, in_files)
By the way, if I use the variable approach instead of the module attribute, than the def blocks of the module in file 2 complain… can’t win? ![]()
Is this because the it inserts the module attribute verbatim as abstract syntax tree? Is there a construct/pattern to handle this case - like would a bind_quote help?
Thank you!
Most Liked
josevalim
Creator of Elixir
Correct. You could instead first unquote it in its actual scope:
quote do
fields = unquote(fields)
defmodule unquote(struct_name) do
use Utils.Access
defstruct fields
end
end
2
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
Other popular topics
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New








