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
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachallaun
Module attributes are expanded during compilation, not runtime, so the
@gs_fieldsin 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_compilecallbacks are called after the rest of the AST of the module has already been generated, so it will run after thelist_attributesfunction is defined and@gs_fieldsis interpolated.shahryarjb
So if I do not call inside
list_attributes, it will delete?, if not how can delete all theattrafter creating struct?Is there a way to see all global @var outside of the module? in
iexfor example??shahryarjb
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
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
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
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
is there a way to delete these? after compile and in runtime? or we should create
evalall the module again to prevent this? just for example I saidjosevalim
Why do you want to delete them? They will disappear anyway after the module is compiled.
shahryarjb
Thanks sir, I thought they will be there after compilation.
I asked it because I saw this link delete all the
attrsof module. it did do any effect? or it is just extra code there without any effecthttps://github.com/ejpcmac/typed_struct/blob/main/lib/typed_struct.ex#L214