metaprogramming


Julia: Building symbol expressions


I have a repetitive set of boilerplate code that looks like this:
type Object
ptr::Ptr{Void}
function Object()
ptr = ccall( (:createObject, LIB_SMILE), Ptr{Void}, ())
smart_p = new(ptr)
finalizer(smart_p, obj -> ccall( (:freeObject, LIB_SMILE), Void, (Ptr{Void},), obj.ptr ))
smart_p
end
end
I would like to auto-generate a set of these type definitions:
for str = ("Obj1","Obj2","Obj3")
op_fname = symbol(str)
op_create = ???
op_free = ???
#eval begin
type $op_fname
ptr::Ptr{Void}
function ($fname)()
ptr = ccall( ($op_create, LIB_SMILE), Ptr{Void}, ())
smart_p = new(ptr)
finalizer(smart_p, obj -> ccall( ($op_free, LIB_SMILE), Void, (Ptr{Void},), obj.ptr ))
smart_p
end
end
end
end
I have not figured out how to generate the correct "symbol symbols" for op_create and op_free. As in, I need op_create = :(:createObj) but I cannot replicate this. Is there a way to generate the needed symbol in this context?
Thank you.
Update: the original answer works (see below), but as #mlubin points out, QuoteNode is an internal implementation function. The quot function in Base.Meta is better:
import Base.Meta.quot
str = "Obj1"
quot(symbol("create$str"))
returns :(:createObj1). But I don't think that Meta.quot is documented either.
Original answer:
You're looking for QuoteNode:
str = "Obj1"
QuoteNode(symbol("create$str"))
returns :(:createObj1) But this seems like a clear application for a macro!

Related Links

Elixir - How can I unquote an array of functions in my macro?
Nim reflect on type's field types at compile-time
Save variables to file at runtime
In Julia, is it possible to pass values for evaluation in an Expr object without using global variables?
Metaprogramming in Julia — Splice integer into variable name
What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
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?

Categories

HOME
grok
time-complexity
telegram
ecmascript-6
serialization
shiro
appcelerator
missing-data
pkcs#11
pattern-matching
fosrestbundle
enthought
mod-security
java.util.logging
ycsb
opengl-es-3.0
php-5.6
intercom
keytool
event-sourcing
scsi
null-pointer
android-7.0-nougat
template-engine
imessage
xmonad
foreign-keys
delphi-xe5
google-maps-autocomplete
poedit
mat
sql-server-2000
fstream
getpixel
openerp-7
parse-tree
alphabetical
openalpr
flatmap
demo
hackintosh
xv6
chronicle-map
android-exoplayer
jboss-4.2.x
ntvs
clarifai
jquery-ui-draggable
posixct
zenhub
arq
subnet
dt
css-transforms
smartfoxserver
stripe-connect
mapxtreme
autosar
winusb
redbeard-ios
pacemaker
adafruit
nikeplus-api
observablecollection
nss
spreadjs
nio2
jaunt-api
assertion
fiware-monitoring
file-move
pypiserver
flock
coda
pax-exam
mmwormhole
uncaught-typeerror
sql-scripts
gii
named-parameters
ora-00904
parallel-testing
live555
ray
terminate
decoupling
getopt
jammer
scringo
ui-select2
route-provider
time.h
mysql-error-2013
dopostback
sentestingkit
uimanageddocument
httponly
infobright
jquery-address
apache-commons-dbutils
lr
event-receiver
codebase
globals
non-clustered-index
maven-1
purepdf
web-application-project
dsl-tools
todos
int64
business-model
dsoframer

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