Why `var!`? - Difference between the two defmacros.

87 views
Skip to first unread message

Jiafeng Cao

unread,
Jul 27, 2016, 9:28:56 AM7/27/16
to elixir-lang-talk
Hi, I've been reading through about macros, and came across the `var!`. I thought I got the idea of it until I wrote some code like this gist.
I know I can use `def unquote(name)(var!(args)), do: ...` to make it work. I just cannot get it. 
What's I missing here? Many thanks.

Eric Meadows-Jönsson

unread,
Jul 28, 2016, 2:23:41 PM7/28/16
to elixir-l...@googlegroups.com
It's documented under "Hygiene for variables" here http://elixir-lang.org/docs/stable/elixir/Kernel.SpecialForms.html#quote/2.

The reasoning for this behaviour is that variables from different contexts shouldn't leak into each others contexts without making it explicit with `var!`.

Your code doesn't work because `args` is used in a different context than where it was defined.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/09af6955-28e3-4a14-8851-0bfa201dc760%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson

Jiafeng Cao

unread,
Jul 28, 2016, 7:40:38 PM7/28/16
to elixir-lang-talk
I have read it several times. From my little experience, I think you mean:

- the `args` is defined inside the macro quote context, and used in the caller context where the quote expands to?
- and that's why var! comes to rescue?

在 2016年7月29日星期五 UTC+8上午2:23:41,Eric Meadows-Jönsson写道:

Eric Meadows-Jönsson

unread,
Jul 29, 2016, 7:18:32 AM7/29/16
to elixir-l...@googlegroups.com
Yes, that is correct. =)


For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson
Reply all
Reply to author
Forward
0 new messages