> Wow, thanks a lot, That one I would never had a chance to figure out.

It's easy enough, you could figure it out yourself too, knowing just a
few tricks.  The essentials are here:
http://docs.julialang.org/en/release-0.4/devdocs/reflection/ plus the
tools: xdump, @less, @edit, @which.

In fact, I had to figure it out myself too, below is approximately what I
did.  Note that this is due to the fact that most of Julia is written in
Julia, thus deep introspection is easy, unlike in many other high-level
languages.

NOTE: that these are internals, which can change anytime and will do so
without warning.  In fact, the trick I showed you will most likely fail
on 0.5.

Julia REPL session:

julia> f(x,y) = (a=5;x+y+a)
f (generic function with 1 method)

julia> ms = methods(f)
# 1 method for generic function "f":
f(x, y) at none:1

julia> xdump(ms)
MethodTable
  name: Symbol f
  defs: Method
    sig: Tuple{Any,Any}::DataType  <: Any
    va: Bool false
    isstaged: Bool false
    tvars: SimpleVector
      length: Int64 0
    func: (anonymous function)
    invokes: Void nothing
    next: Void nothing
  cache: Void nothing
  cache_arg1: Void nothing
  cache_targ: Void nothing
  max_args: Int64 2
  kwsorter: #undef
  module: Module Main


### Not sure why below xdump does not work, instead look at the fields directly:

julia> xdump(ms.defs.func)
ERROR: ArgumentError: invalid arguments to xdump
 in xdump at show.jl:936

julia> fieldnames(ms.defs.func)
3-element Array{Symbol,1}:
 :fptr
 :env
 :code

#### This is it.  Except that it is not an Expr?!

julia> ms.defs.func.code
AST(:($(Expr(:lambda, Any[:x,:y], 
Any[Any[Any[:x,:Any,0],Any[:y,:Any,0],Any[:a,:Any,18]],Any[],0,Any[]], :(begin  
# none, line 1:
        a = 5
        return x + y + a
    end)))))

julia> typeof(ms.defs.func.code)
LambdaStaticData

#### This is just gibberish:

julia> ms.defs.func.code.ast
102-element Array{UInt8,1}:
 0x1d
 0x08
 0x03
 0x23
...

### But it is displayed, thus look at how it is done. (if @edit
### doesn't work then use @less. Also @which is useful, which tells you
### where to look.)

julia> @edit show(ms.defs.func.code)

### Returns this wrapper: show(x) = show(STDOUT::IO, x)
### So check what gets called:

julia> @edit show(STDOUT, ms.defs.func.code)

### Ah, Base.uncompressed_ast transforms it into an Expr:

julia> Base.uncompressed_ast(ms.defs.func.code)
:($(Expr(:lambda, Any[:x,:y], 
Any[Any[Any[:x,:Any,0],Any[:y,:Any,0],Any[:a,:Any,18]],Any[],0,Any[]], :(begin  
# none, line 1:
        a = 5
        return x + y + a
    end))))

Reply via email to