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

Remove a const qualifier from a variable in D
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

Categories

HOME
httparty
kentico
localization
ggplot2
wakanda
angular2-material
serialization
solver
knitr
gradient
vscode-extensions
replace
opendj
onclick
settings
mapbox
qpython
plist
ycsb
comsol
symbol
cgbitmapcontext
object-storage
moses
pylons
genexus-sd
mustache
reactjs.net
arduino-ide
hivemq
salesforce-communities
metaprogramming
signals-slots
resolution
plaintext
vaadin-charts
opencmis
wcftestclient
hammer.js
yii2-api
globalize
tinymce-3
corruption
spring-data-couchbase
truezip
cd-burning
vibrate
structuremap4
android-instrumentation
testrail
meteor-packages
lrs
simevents
http-status-code-401
phpwebsocket
aspen
qbxml
atan2
character-replacement
poppler
google-refine
snackbar
qtwebengine
named-parameters
ignite-ui
grails-plugin-rabbitmq
memset
sbcl
hibernate-entitymanager
sphinxql
model-driven-development
modular
printdialog
poker
octal
http-status-code-410
asplinkbutton
redis-py
appjs
fitbounds
luabind
denied
google-floodlight
mbeans
strsep
cruisecontrol.rb
opml
blender-2.49
feasibility
xpsdocument
xslcompiledtransform

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