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

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

Categories

HOME
google-app-engine
embedded
httparty
search
pclxl
replace
bitbucket-api
google-cloud-functions
static-site
yandex
switch-statement
big-o
polling
spring-session
pkcs#11
joomla3.0
okhttp
pattern-matching
arangodb
dkim
endeca
pega
why3
ycsb
filechannel
selenium-ide
aws-sdk
jersey-1.0
backpack-for-laravel
dataflow-diagram
postscript
scriptella
short-url
jackson-modules
has-and-belongs-to-many
realm-mobile-platform
custom-controls
zend-debugger
dsx-desktop
classcastexception
reactjs.net
jwplayer6
sessionstorage
css-position
spring-mvc-test
setup.py
trigonometry
mesosphere
gitlist
robomongo
reportservice2010
payara-micro
arq
custom-font
appdomain
sundials
er-diagram
structuremap4
filenet
autosar
seek
cifilter
http-status-code-401
ikiwiki
cycle2
ascii-art
directdraw
grgit
lazybones
zen-cart
botan
mraid
umlgraph
xmltype
angular-gettext
boost-program-options
encryption-asymmetric
xml-namespaces
create.js
unix-socket
lambda-architecture
functional-java
grails-plugin-rabbitmq
menubar
high-resolution
gtm-oauth2
model-driven-development
hidapi
inappsettingskit
nebula
video-codecs
dopostback
tree-balancing
midlet
urlrewriter
role
massive
timthumb
event-receiver
photolibrary
jqueryform
wordprocessingml
interop-domino

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