metaprogramming - Julia: Building symbol expressions -
metaprogramming - Julia: Building symbol expressions -
i have repetitive set of boilerplate code looks 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 auto-generate 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 generate right "symbol symbols" op_create , op_free. in, need op_create = :(:createobj)
cannot replicate this. there way generate needed symbol in context?
thank you.
update: original reply works (see below), @mlubin points out, quotenode
internal implementation function. quot
function in base.meta
better:
import base.meta.quot str = "obj1" quot(symbol("create$str"))
returns :(:createobj1)
. don't think meta.quot documented either.
original answer: you're looking quotenode
:
str = "obj1" quotenode(symbol("create$str"))
returns :(:createobj1)
seems clear application macro!
metaprogramming julia-lang
Comments
Post a Comment