Retrieve method content as an `Expr`ession

后端 未结 2 434
长发绾君心
长发绾君心 2021-01-14 16:06

I have a function f defined as follows.

f(x, y) = 3x^2 + x*y - 2y + 1

How can I retrieve the following quote bloc

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 16:32

    First, retrieve the method using methods(f).

    julia> methods(f)
    # 1 method for generic function "f":
    f(x, y) at REPL[1]:1
    
    julia> methods(f).ms
    1-element Array{Method,1}:
     f(x, y) at REPL[1]:1
    
    julia> method = methods(f).ms[1]
    f(x, y) at REPL[1]:1
    

    From here, retrieving the Expression is straightforward; simply use the lambda_template attribute of the method.

    julia> method.lambda_template
    LambdaInfo template for f(x, y) at REPL[1]:1
    :(begin 
            nothing
            return ((3 * x ^ 2 + x * y) - 2 * y) + 1
        end)
    

    Edit: This does not work in Julia v0.6+!

提交回复
热议问题