julia lang - How to alter an expression in generic function? -


e.g.

function sq(x)     x ^ 2 end  function sq2(x)     (x+1) ^ 2 end  function fun(x)     sq(x) end 

i replace sq call sq2 call redefine fun generic function. attempt below changes call wasn't able redefine function. appreciated.

change(:fun, (int,))  function analyze_expr(exp::expr)    = 1:length(exp.args)       arg = exp.args[i]       if(typeof(arg) == expr)          analyze_expr(arg)       elseif(arg==symbol("sq"))          exp.args[i] = symbol("sq2")       end    end 

end

function change(sym::symbol, params)     func = eval(sym)     func_code = code_lowered(func, params)     func_body = func_code[1].args[3]     analyze_expr(func_body)     println("printing function body:",func_body) end 

i suspect you'll find easier kind of work using macros: http://docs.julialang.org/en/latest/manual/metaprogramming/

given existing function definition, relevant thing in julia isn't syntax generated compiled machine code resulted. knowledge, modifying syntax won't (without deep hacking in julia's internals) have effect on compiled machine code.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -