linusdm
Is sigil_H an example of a macro that is unhygienic?
After reading Metaprogramming Elixir, and the hexdocs on metaprogramming and macros, I was wondering how this ties into macros we use everyday.
The examples about hygiene of macros show how you can use var!/2 to explicitly reach out to the variables declared in the caller site and alter them. Doing so is considered unhygienic. It’s not forbidden but it can confuse the user of the macro.
I’m wondering if just reading a variable in a macro with var1/2 is also considered unhygienic.
The sigil_H macro in LiveView does exactly this. it gives a runtime error if you try to use the sigil, without a variable assigns available (probably just for better developer experience, to avoid a more generic error if the variable is not found). The assigns variable is like an extra input parameter for the macro (sigils only accept the string and a modifier so there is no other way to use sigils in this way).
I totally see how this is a good design decision. If used sparingly, this is the way to go. Just reflecting and wondering if I connected the dots correctly here.
Most Liked
josevalim
That’s a good question. As you said:
The examples about hygiene of macros show how you can use
var!/2to explicitly reach out to the variables declared in the caller site and alter them.
Emphasis mine. Is it unhygienic if it doesn’t alter variables, only reads them?
linusdm
After having let it simmer for a while, I think the biggest reason why read unhygienic is less of a problem, is because it will raise an error at compile-time, when the context of the macro doesn’t provide the implicitly read variable (is this always the case? I might be wrong here). So the implicitness becomes explicit, when you try to compile such code
It never gets to be compiled, luckily.
Whereas write unhygienic code can go unnoticed, which is a big problem, if you weren’t expecting that.
So this is what I get to:
hygienic > read hygienic (won’t compile if expectation about variable is not met) > write hygienic (can go unnoticed)
My understanding of macro hygiene is becoming more solid from this thought exercise, so thanks!
sodapopcan
Interesting. I also blew passed the “alter” part and always thought of ~H as unhygenic since you must have a seemingly “magic” binding to exist for it to work! And yet, once in a while I write some helper macros that must be used in a specific context to work and I never think of them as unhygenic.. huh. How 'bout that!
Popular in Discussions
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









