Got it to work myself, by creating an array of expressions outside the
inner function generation, containing insert! calls adding the
subexpressions at fdef.args[1].args[2] (just before the first line of code
in the function).
The issue of parse() behaviour not aligning with :() still stands, but I
don't rely on it anymore.
Op dinsdag 6 mei 2014 11:34:39 UTC+2 schreef 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
>
>