metaprogramming


Inserting an AST into a quote do end block without unquoting it


Using quote do end with unquote normally provides a super easy way to inject values into an AST, allowing me to avoid traversing it.
Here is one such situation.
ast = quote do 1 + 1 end
quote do
unquote(ast)
|> manipulate_number
end
However, if I want to insert not the result of the quoted expression 1 + 1, but the AST {:+, [context: Elixir, import: Kernel], [1, 1]}, I find myself wrapping the AST in the AST of another call to quote do end, like so:
ast1 = quote do 1 + 1 end
ast2 = {:quote, [], [[do: ast1]]}
quote do
unquote(ast2)
|> manipulate_number
end
Is there another way to do this?
If you want to inject the AST as is into another quoted expression, you want Macro.escape/1. Another way to put it, you can use Macro.escape/1 to ensure some quoted expression will return itself when evaluated/expanded.
If I understand your problem correctly, the following should do what you desire:
defmodule MacroExample do
defmacro example(ast) do
quoted = quote bind_quoted: [ast: ast] do
ast + 3
end
IO.inspect Macro.to_string(quoted)
quoted
end
end
defmodule MacroTest do
require MacroExample
IO.inspect MacroExample.example(1 + 1)
end
And the result in iex:
iex> c "test.ex"
"(\n ast = 1 + 1\n ast + 3\n)"
5
I'm guessing that your specific problem is likely more complicated than just deferring the addition operation though, if the above isn't what you are looking for, perhaps a more realistic example of what you are trying to accomplish could clarify the problem and it's solution.

Related Links

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
Runtime meta programming in ceylon
Is there a fast way of going from a symbol to a function call in Julia? [duplicate]

Categories

HOME
facebook-oauth
adal
api
eslint
zend-framework
interface
serialization
opendj
big-o
zip
vert.x
trac
boto3
flume
character
teechart
extjs4.2
32bit-64bit
categorical-data
activecollab
redis-sentinel
selectize.js
libusb
modbus-tcp
intershop
red5
windows-xp
sar
parcelable
oculus
quantlib
cppreference
uitapgesturerecognizer
sqlpackage
boost-asio
recurring-events
dumpbin
quickfixj
hibernate-search
ideascript
google-data-api
spring-mvc-test
skylink
naming
logback-groovy
dapper-simplecrud
dynamic-linq
ovf
activeperl
magic-draw
jqchart
filenet
donations
winusb
latin
envi
google-cloud-console
jquery-jscrollpane
swizzling
kango-framework
deep
thread-sleep
np-spring
ess
page.js
text-search
vacuum
liquid-layout
actionfilterattribute
ejb-2.x
entry-point
angular-carousel
named-parameters
lambda-architecture
cancellation
mutual-authentication
maven-scm
syndicationfeed
accesscontrolexception
bin
oocss
mongohq
cnf
diem
sqlclr
rmiregistry
urlrewriter
ikimagebrowserview
jquery-tools
saxparseexception
tournament
office-2007
dot-emacs
filesystemobject
web-application-project
cuda.net
mathematical-typesetting
wspbuilder
pnrp

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