metaprogramming


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.

Related Links

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

Categories

HOME
turing-machines
fuelux
redux-form
yahoo-finance
sh
soa
parameters
hpc
jtextpane
extjs4.2
heap
naivebayes
nancy
package-managers
glyphicons
esoteric-languages
smartclient
googlebot
ksh
cgbitmapcontext
systemtime
joptionpane
froala
jackson-modules
oculus
bringtofront
boost-asio
google-cast
dumpbin
laravel-eloquent
wacom
hunspell
carriage-return
installscript
html-encode
arduino-ide
numpy-broadcasting
raft
reason
openproject
connection-pool
odroid
hackintosh
comparison-operators
dbnull
plr
outputcache
puredata
configurationmanager
google-cse
page-replacement
tinkerpop
boxing
compiler-design
git-flow
pdf-conversion
gpg-signature
rocks
stripe-connect
usart
xcode-server
iotivity
flex4.6
dotnetnuke-7
greatest-n-per-group
dup
vorpal.js
deep
hadoop-partitioning
maybe
grgit
computer-architecture
include-guards
jdk1.7
alloy-ui
snoop
ejb-2.x
browserify-shim
pseudo-class
punycode
entry-point
machine-instruction
device-tree
personalization
mintty
matrix-inverse
dct
gnumeric
buffering
mutual-authentication
fpdi
openargs
rfc5322
mysql-error-1044
jammer
oocss
modular
nokia-s40
pyunit
subgraph
window-decoration
denied
landscape-portrait
update-statement
pylucene
rmdir
http-daemon
luajava
folding
purepdf
firefox-3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App