Created
August 29, 2020 20:14
-
-
Save henrik/29030710ff7b0c8379204fdd7017c2fc to your computer and use it in GitHub Desktop.
Elixir "@foo bar" macro example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyMacro do | |
defmacro @{name, _meta, [arg]} do | |
IO.inspect "Your #{name} is a #{arg}" | |
end | |
defmacro __using__(_) do | |
quote do | |
import Kernel, except: [@: 1] | |
import unquote(__MODULE__) | |
end | |
end | |
end | |
defmodule Example do | |
use MyMacro | |
@fruit "banana" | |
# Outputs "Your fruit is a banana" at compile time. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment