How to return literals with macros?

Hello,

Thanks for the details!

For me a literal is a “constant” but in its context, for example:

a = "x"
x = "x"

Here in the first line a is obviously a variable, and x is a literal.
In the seconde line the lhs x is also a variable when the rhs x is a literal.

However in my case, when I want to end up from

my_func(arbitrary_func(arbitrary_parameters))

to

@x && arbitrary_func(arbitrary_parameters))

I want to end up with @x used as a literal in the code in which it will end up to what it means in the runtime, i.e. the assign @x.

To be more clear, let’s assume I have this code in all my view files:

custom_log(IO.inspect value)
..
custom_log(error_message)
...
custom_log(IO.inspect(reason, label: "Reason"))
custom_log(IO.inspect reason, label: "Reason")

(I deliberately put different things)

Now I can “preprocess” that file with a script in any language.
Here an example in JS with a simple regex replace:

//Simple example working only if there is at most one nested parentheses, but that''s okay in my case
my_file_content.replace(/custom_log\((.*\)?\))/g, "@x && $1")

In C it’s easy to do so with macros and #define.
I think that it’s something easily doable with elixir Macro…

Really it’s just static string replacement…
I don’t know how to explain this in other words…

Anyway, I tried with your example and some other variants using var.
And I got the same error as assigns not being available (expanded as assigns() and function undefined)

As I said, my current use case is to use it in that form with helper functions in LiveView views.
The purpose of @x && whatever is that here x is something like a fingerprint and when it’s changing I force the rerendering of whatever.

See this message on another topic for more context…

I hope my intent would be clear right now…