Useful functions for working with the Julia Expr
type.
Constructs a new expression similar to e
, but having had the function
f
applied to every leaf.
julia> map(x -> isa(x, Int) ? x + 1 : x, :(1 + 1))
# => :(+(2,2))
Recursively walk an expression, applying a function f
to each
subexpression and leaf in e
. If the function application returns an
expression, that expression will be walked as well. The function can
return the special type ExpressionUtils.Remove
to indicate that a
subexpression should be omitted.
julia> b = quote
let x=1, y=2, z=3
x + y + z
end
end
# => quote # none, line 2:
# let x = 1, y = 2, z = 3 # line 3:
# x + y + z
# end
# end
julia> remove_line_nodes(node::LineNumberNode) = ExpressionUtils.Remove
# remove_line_nodes (generic function with 1 method)
julia> remove_line_nodes(ex) = ex
# remove_line_nodes (generic function with 2 methods)
julia> walk(remove_line_nodes, b)
# => quote
# let x = 1, y = 2, z = 3
# x + y + z
# end
# end
Syntax rewriting!
julia> ex = quote
let x, y, z
bar
x + y
y + z
end
end
julia> template = quote
let _SPLAT_bindings_
_funname_
_SPLAT_body_
end
end
julia> out = quote
function _funname_( _UNSPLAT_bindings_)
_UNSPLAT_body_
end
end
julia> fnexpr = expr_replace(ex, template, out)
# => :(function bar(x, y, z)
# x + y
# y + z
# end)
julia> eval(fnexpr)
# bar (generic function with 1 method)
julia> bar(1, 2, 3)
# => 5
Plays well with macros. See ValueDispatch.jl for another example.
This package contains a range of utilities for working with expressions that contain function definitions. Each of these has its own docstring that goes into detail, so here is just a summary:
is_funcdef_expr
tests whether an expression is a function definitionget_funcdef_expr
returns the "inner" function definition expression, even if wrapped inside an@inline
or:block
expressionfuncdef_longform
cannonicalizes function definition expressionsfuncdef_name
returns the function's name Symbolfuncdef_params
returns the vector of parameters for a parametric functionfuncdef_args
returns the vector of arguments (names + type declarations)funcdef_argnames
returns the vector of argument names (Symbols)funcdef_argtypeexprs
returns the vector of argument type declarations