Base.Cartesian has an outstanding function lreplace , which may be what you need. Then you can do something like:
julia> values = Dict(:A=>3, :B=>1) Dict{Symbol,Int64} with 2 entries: :B => 1 :A => 3 julia> import Base.Cartesian.lreplace julia> expr = :(2*A) :(2A) julia> function lreplace_all(expr, d) for (k, v) in d expr = lreplace(expr, k, v) end expr end lreplace_all (generic function with 1 method) julia> lreplace_all(expr, values) :(2 * 3) julia> @eval foo(A) = $(lreplace_all(:(2A), values)) foo (generic function with 1 method) julia> foo(1) 6
Although, since A is determined by the values dict, it makes sense to define foo as a function with a null argument (unless I missed something).
EDIT: After re-reading your question, it seems like you want to go into the actual dictionary for the function, and not have the values ββavailable at compile time, as I did above. In this case, we get a little creative:
First we need a lreplace function that will work with expressions that are light enough
julia> dictreplace!(ex, s, v) = ex dictreplace! (generic function with 1 method) julia> dictreplace!(ex::Symbol, s, v) = s == ex ? v : ex dictreplace! (generic function with 2 methods) julia> function dictreplace!(ex::Expr, s, v) for i=1:length(ex.args) ex.args[i] = dictreplace!(ex.args[i], s, v) end ex end dictreplace! (generic function with 3 methods) julia> dictreplace(ex, s, v) = dictreplace!(copy(ex), s, v) dictreplace (generic function with 1 method)
Now we want to replace each occurrence of a symbol in our keys with a dictionary search key
julia> function dictreplace_all(expr, kys, dsym) for k in kys expr = dictreplace(expr, k, :($(dsym)[$(QuoteNode(k))])) end expr end dictreplace_all (generic function with 1 method) julia> dictreplace_all(:(2A), keys(values), :d) :(2 * d[:A]) julia> @eval foo(args) = $(dictreplace_all(:(2A), keys(values), :args)) foo (generic function with 1 method) julia> values[:A] = -99 -99 julia> foo(values) -198