shahryarjb

shahryarjb

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

Showing Posts 1 to 10

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.

shahryarjb

shahryarjb OP

So if I do not call inside list_attributes, it will delete?, if not how can delete all the attr after creating struct?

Is there a way to see all global @var outside of the module? in iex for example??

shahryarjb

shahryarjb OP

As I understand, it exists because I call it? if I do not call it, it will be deleted?, and is there a way to prevent?

Another question, it can increase module size after compile? I just want to delete unnecessary things from my module

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.

shahryarjb

shahryarjb OP

So why should I delete it in this example? it does not matter exists or not?

https://github.com/ejpcmac/typed_struct/blob/main/lib/typed_struct.ex#L214

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.

shahryarjb

shahryarjb OP

is there a way to delete these? after compile and in runtime? or we should create eval all the module again to prevent this? just for example I said

josevalim

josevalim

Creator of Elixir

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

shahryarjb

shahryarjb OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews