Not sure if this is what you're looking for, but if you have a generic 
function, you can (kind of) pluck out the body of the specified method as 
follows:

julia> f(x::Int) = x+5
f (generic function with 1 method) 

julia> e = code_typed(f, (Int,)) 
1-element Array{Any,1}: 
 :($(Expr(:lambda, Any[:x], Any[Any[Any[:x,Int64,0]],Any[],Any[],Any[]], :(
begin  # none, line 1: 
        return (top(box))(Int64,(top(add_int))(x::Int64,5)) 
    end::Int64)))) 

julia> dump(e) 
Array(Any,(1,)) 
  1: Expr  
    head: Symbol lambda 
    args: Array(Any,(3,)) 
      1: Array(Any,(1,)) 
        1: Symbol x 
      2: Array(Any,(4,)) 
        1: Array(Any,(1,)) 
          1: Array(Any,(3,)) 
        2: Array(Any,(0,)) 
        3: Array(Any,(0,)) 
        4: Array(Any,(0,)) 
      3: Expr  
        head: Symbol body 
        args: Array(Any,(2,)) 
          1: Expr  
          2: Expr  
        typ: Int64 
    typ: Any 

julia> f_body = e[1].args[3] 
:(begin  # none, line 1: 
        return (top(box))(Int64,(top(add_int))(x::Int64,5)) 
    end::Int64)


f_body can then be inspected, though clearly it contains more than what it 
started with. It will act as the body of a function when eval'd, though I'm 
sure this is *not* the best way to achieve such a result:

julia> x = 5
5 

julia> eval(f_body) 
10


See http://docs.julialang.org/en/latest/manual/metaprogramming/.

Note also the above doesn't work on anonymous functions. Actually, if 
somebody *does* know how to inspect code from an anonymous function without 
going so deep as the `code` field, I would really love to hear it.

On Sunday, June 21, 2015 at 1:31:22 AM UTC-4, Jiyin Yiyong wrote:
>
> As described in http://blog.leahhanson.us/julia-introspects.html Julia 
> parses code to AST.
> But is there function provided to generate code back?
>
> I checked on Google and the docs, but found nothing so far.
> http://docs.julialang.org/en/release-0.3/stdlib/base/
>
>

Reply via email to