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

CoffeeScript dynamic methods
Tool for automated porting and language that can compile into others
HAML meta-programming
Computability & complexity application [closed]
REBOL metaprogramming questions
How do I replace a message body programmatically in Smalltalk?
Are there real world applications that use metaprogramming?
How to download MQL4 IDE and Editor?
Can I add numbers with the C/C++ preprocessor?
Meta Programming, whats it good for?
Using metamorphic code to reduce boilerplate
Typed metaprogramming languages [closed]
What to read as a good intro and quickstart to aspect-oriented programming and metaprogramming?
Examples of what D’s templates can be used for
Using Polymorphic Code for Legitimate Purposes?
A guide to Boo's metaprogramming and extensibility features?

Categories

HOME
scikit-learn
developer-tools
forms
webdriver
jax-rs
angular-formly
nuget
vxml
hpc
crystal-lang
syntaxnet
fabric.io
mxgraph
selenium-ide
zerobrane
libusb
coin3d
atlassian-stash
dimensional-modeling
template-engine
shortest-path
asp.net-core-webapi
google-maps-autocomplete
drive
blade
interpreter
pic32
licensing
rpgle
panoramas
glib
fstream
erb
hreflang
github3.py
classcastexception
activexobject
openwhisk
mnist
xajax
reason
sessionstorage
chef-solo
xv6
naming
strftime
plan-9
httrack
mongotemplate
bbpress
sandcastle
chromium-os
actor-platform
webjars
flex4.6
rspec2
avspeechsynthesizer
bizagi
phpwebsocket
rasterize
boomerang
opserver
text-search
visual-sourcesafe-2005
haskell-warp
mirror
teamcity-9.1
nsurlsessiondatatask
variadic-templates
rapidsvn
sonarqube-5.0
webfinger
entry-point
imagefilter
tweenlite
rmysql
encryption-asymmetric
gii
xml-namespaces
ora-00904
reflector
android-usb
solace-mq
embedded-fonts
windows-phone-8-sdk
modular
rolify
nebula
kaleidoscope
composite-component
cardreader
jdb
azman
entity-attribute-value
update-statement
jqote
rmdir
quotation-marks
codebase
uideviceorientation
rowlex
ajax-polling
echo3
glui
lobo-cobra
wordprocessingml
google-wave
great-circle

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