Also, on a related note, is it possible to create a macro which transforms 
a function, but makes the result accessible in the caller's package? e.g.
module Test

export @transform

macro transform(ex)
    eval(ex)
end

end

using Test

@transform function foobar()
    println("foobar")
end

function main()
    foobar()
end

main()

This testcase doesn't work due to foobar being defined in the Test package. 
I'd want to avoid having to call Test.foobar().

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
>
>

Reply via email to