> Is it also possible to get a list of names of the variables used in a
> function?
>
> e.g.  for
>
> function f(x,y)
>     k=0.1
>     return x*y+k
> end
>
> I'd like to get a list ["k","x","y"]
>
> My first thought was to make a method f() that returns this list, but if
> its possible to do this otherwise and more generally that would be very
> useful

You'd have to inspect the AST.  Looks like there is indeed a list in there:

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

julia> methods(f).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)))))

You can get at it with:

julia> ast = Base.uncompressed_ast(methods(f).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))))

julia> ast.args[2][1]
3-element Array{Any,1}:
 Any[:x,:Any,0]
 Any[:y,:Any,0]
 Any[:a,:Any,18]

Reply via email to