metaprogramming

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

metaprogramming

Andreas Lobinger
Hello,

i'm not the master of metaprogramming, but i have the feeling i could use some for library interfacing (ccall etc).
So i'm trying to build a macro that outputs an expression containing a function definition.

1) Are there known examples (in julia or a package) that actually do this?
2) I somehow can read the Expr build by anonymous functions, but how for named functions?

julia> e1 = function(c::Int)
       c*2
       end
(anonymous function)

julia>

julia> e1
(anonymous function)

julia> e1(1)
2

julia> e1.code
AST(:($(Expr(:lambda, {:(c::Int)}, {{},{{:c,:Any,18}},{}}, :(begin
        c = top(typeassert)(c,Int) # none, line 2:
        return c * 2
    end)))))

julia> function e2(c::Int)
       c*2
       end
e2 (generic function with 1 method)

julia> e2.code
ERROR: access to undefined reference

Wishing a happy day,
        Andreas
Reply | Threaded
Open this post in threaded view
|

Re: metaprogramming

Ivar Nesje

e2.env.defs.func.code


kl. 13:25:10 UTC+2 fredag 1. august 2014 skrev Andreas Lobinger følgende:
Hello,

i'm not the master of metaprogramming, but i have the feeling i could use some for library interfacing (ccall etc).
So i'm trying to build a macro that outputs an expression containing a function definition.

1) Are there known examples (in julia or a package) that actually do this?
2) I somehow can read the Expr build by anonymous functions, but how for named functions?

julia> e1 = function(c::Int)
       c*2
       end
(anonymous function)

julia>

julia> e1
(anonymous function)

julia> e1(1)
2

julia> e1.code
AST(:($(Expr(:lambda, {:(c::Int)}, {{},{{:c,:Any,18}},{}}, :(begin
        c = top(typeassert)(c,Int) # none, line 2:
        return c * 2
    end)))))

julia> function e2(c::Int)
       c*2
       end
e2 (generic function with 1 method)

julia> e2.code
ERROR: access to undefined reference

Wishing a happy day,
        Andreas