shahryarjb

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

josevalim

Creator of Elixir

Why do you want to delete them? They will disappear anyway after the module is compiled.

zachallaun

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

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.

D4no0

D4no0

Attributes are present only before your module is compiled, as mentioned above, there is no way to get attributes at runtime as they don’t exist anymore in runtime context, their usage is replaced with their contents.

D4no0

D4no0

This is obviously deleting the attributes to follow some internal logic, I am too tired now to read these macro abominations. The only reason you would delete attributes besides for a logic flow, would be to free memory when compiling, but I doubt this is the case here.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
alice
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
gonzofish
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
vonH
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
LegitStack
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement