linusdm

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

josevalim

Creator of Elixir

That’s a good question. As you said:

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.

Emphasis mine. Is it unhygienic if it doesn’t alter variables, only reads them?

linusdm

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 :slight_smile: 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

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!

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

We're in Beta

About us Mission Statement