How to define functions based on attribute to elixir?
Let's say I have a modules Silent and Definer. I want to define a couple of functions for Silent, based on its attribute. Let me explain: defmodule Silent do #function_names [:a, :b, :c] use Definer end defmodule Definer do defmacro __using__(_) do quote do Enum.each(#function_names, fn(n) -> def unquote(n)() do # line 5 IO.puts "a new method is here!" end end) end end end But this approach actually doesn't work because I have undefined function n/0 on line 5. How can I implement desired functionality?
You need to pass unquote: false to quote in Definer.__using__/1 to be able to inject an unquote fragment inside a quote. defmodule Definer do defmacro __using__(_) do quote unquote: false do Enum.each(#function_names, fn(n) -> def unquote(n)() do # line 5 IO.puts "a new method is here!" end end) end end end defmodule Silent do #function_names [:a, :b, :c] use Definer end Silent.a Silent.b Silent.c prints a new method is here! a new method is here! a new method is here! A similar case is documented in detail in the Kernel.SpecialForms.quote/2 docs which also mentions how to use bind_quoted if you want to both inject some variables into a quote and create unquote fragments.
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?
Multiple #:build macros
Julia automatically generate functions and export them
Dealing with end keyword in macros for array indices
Implementation of AnyMap and runtime overhead of `struct Port(u32);`
Can function application be overriden with Macro in Elixir?
“Registering” trait implementations + factory method for trait objects
Statement list composition