Yes, that's right. I actually wanted to get the AST to contain some node
representing $(length(data)), so I could get the following example to work:
function main()
data = [1 2 3]
ex = parse("return $(length(data))")
data = [1 2 3 4]
@assert eval(ex) == 4
end
main()
but as far as I understand now this is not possible.
Also, I know all this sounds very contrived and overly complicated, but I'm
playing with some @staged-like macro specializing function based on some
expressions.
Op dinsdag 6 mei 2014 15:26:21 UTC+2 schreef Simon Danisch:
>
> Well, I'm not sure if I understand your problem correctly, but it works
> quite similar.
> You just don't need to escape the $ for string interpolation:
> eval(parse("println($(length(data)))"))
>
> Am Dienstag, 6. Mai 2014 11:34:39 UTC+2 schrieb Tim Besard:
>>
>> 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
>>
>>