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.
How can I call .WHY on a subroutine in a class in Perl 6?
Using Roslyn, if I have an IdentifierNameSyntax, can I find the member type it refers to (field, property, method…)
Elixir - Define functions in module by metaprogramming
Disambiguate the various quoting mechanisms in Julia metaprogramming
How to initialize list using macros and then generate same set of functions in 2 different modules?
Type-safe metaprogramming? Using strings to query ceylon.language for meta information?
Interpolating an Expression into an Expression
Loop unrolling? in Julia with metaprogramming
Macro for a function template: escaping issues
Macro that defines macro returning struct constant
Array of Expressions to an Array Definition
Code generation from restricted set of input
System of equations using metaprogramming
How to define a partial copy constructor metaprogramically in Julia?
How to interpolate into a Julia “for” expression?
Sending a keyword list to a macro and using bind_quoted