Aha, thanks for that tip about esc(), my example now works correctly. Also, when venturing in macro land I came across examples/staged.jl, which doesn't seem to work with 0.3 anymore (it does with 0.2). What is the policy on that, should I open an issue? It seems that the let-wrapping on line 24 causes a malformed expression error.
Op dinsdag 6 mei 2014 17:23:06 UTC+2 schreef Jameson: > > Your first example would be better / easier / clearer / faster of you use > an anonymous closure function instead of eval > function f(a) > g=()->length(a) > println(g()) > end > > A macro should not call eval. Instead return esc(ex) to make you code > functional > > > On Tuesday, May 6, 2014, Tim Besard <[email protected] <javascript:>> > wrote: > >> 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 = >>> >>
