I'm trying something with macro's, and I can't understand the following
behavior:
julia> data = [1 2 3]
1x3 Array{Int64,2}:
1 2 3
julia> eval(:(println($(length(data)))))
3
julia> eval(parse("println(\$(length(data)))"))
ERROR: unsupported or misplaced expression $
Why do these behave differently?
Placed in context, I'm trying to generate a function from within macro,
which on its turn generates an expression containing a the result of a
subexpression evaluated when the function was called. Or, in code:
macro outer(ex)
ex = Expr(:quote, :(println($ex)))
fdef = quote
function inner(data)
$ex
end
end
eval(fdef)
end
function inner_wanted(data)
:(println($(length(data))))
end
function main()
@outer(length(data))
data = [1 2 3]
println("What I want:")
ex = inner_wanted(data)
println(ex)
eval(ex)
println("\n\nWhat I have:")
ex = inner(data)
println(ex)
eval(ex)
end
main()
The problem here is that I cannot seem to generate ":($(something))" within
a quote block... I even tried parse()ing that very construct, as seen in
the beginning of this mail, but even that fails. Can anybody help me out?
Thanks