defmodule Mod do
defmacro definfo do
IO.puts("In macro's context (#{__MODULE__})")
quote do
IO.puts("In caller's #{@attr} context (#{__MODULE__})")
def friendly_info do
IO.puts """
My name is #{__MODULE__}
My functions are #{inspect __MODULE__.__info__(:functions)}
"""
end
end
end
end
defmodule MyModule do
@attr "Hello"
require Mod
Mod.definfo()
end
I’m currently reading Metaprogramming in Elixir, I understand the concept of macro hygiene, I was just wondering why elixir will allow access of a caller’s module attribute (in the above modified example, I can access the value of @attr) within the macro but frowns on accessing the caller’s variables unless explicitly indicated via var!
since I presume both could be missing at compile time and yes I understand elixir will return nil if an attribute is missing. A bit of education will help.