shahryarjb
@before_compile with Module.delete_attribute do not delete @var
Hello, Based on typed_struct/lib/typed_struct.ex at main · ejpcmac/typed_struct · GitHub , I am practicing to learn more about macro in elixir, all the things are good but I can not delete the attr I register in module.
For example this is my macro
@temporary_revaluation [
:gs_fields,
:gs_types,
:gs_enforce_keys
]
defmacro guardedstruct(opts \\ [], do: block) do
ast = register_struct(block, opts)
quote do
(fn -> unquote(ast) end).()
end
end
def register_struct(block, opts) do
quote do
Enum.each(unquote(@temporary_revaluation), fn attr ->
Module.register_attribute(__MODULE__, attr, accumulate: true)
end)
Module.put_attribute(__MODULE__, :gs_enforce?, unquote(!!opts[:enforce]))
@before_compile {unquote(__MODULE__), :delete_temporary_revaluation}
...
end
end
So the macro deletes attrs is:
defmacro delete_temporary_revaluation(%Macro.Env{module: module}) do
Enum.each(unquote(@temporary_revaluation), &Module.delete_attribute(module, &1))
end
With these codes I try to delete module attributes. but inside the module I use this macro, I can still access to all the
@temporary_revaluation [
:gs_fields,
:gs_types,
:gs_enforce_keys
]
For example:
defmodule MishkaPub.ActivityStream.Type.Object do
use GuardedStruct
guardedstruct do
field(:id, String.t())
field(:type, String.t())
end
def list_attributes do
@gs_fields
end
end
As you see I can print @gs_fields. but inside the macro I tell delete all of them before compile!!
I want to handle this inside macro not the module
Please help me to find what is the problem?
Full code:
Thank you in advance
Most Liked
josevalim
zachallaun
Module attributes are expanded during compilation, not runtime, so the @gs_fields in your function call isn’t actually referencing an attribute when you call it, it’s the literal value that was inserted into the AST.
al2o3cr
@before_compile callbacks are called after the rest of the AST of the module has already been generated, so it will run after the list_attributes function is defined and @gs_fields is interpolated.
Last Post!
shahryarjb
Thanks sir, I thought they will be there after compilation.
I asked it because I saw this link delete all the attrs of module. it did do any effect? or it is just extra code there without any effect
https://github.com/ejpcmac/typed_struct/blob/main/lib/typed_struct.ex#L214
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









