On Saturday, January 24, 2015 at 12:48:04 PM UTC+10, Kirill Ignatiev wrote:
>
> With this macro:
>
> macro test(fun)
>   @printf "%s\n" string(fun)
> end
>
> these three expressions print as follows:
>
> @test(sin)
> # sin
> @test(x -> sin(x))
> # x->begin  # /***.jl, line 84:
> #        sin(x)
> #    end
> @printf "%s\n" string(x -> sin(x))
> # (anonymous function)
>
> How do I get the string "(anonymous function)" inside a macro with the 
> string function? It keeps expanding fun to the whole function expression, 
> which I don't want to print in full.
>

Macros receive expressions as arguments, not the object they evaluate to. 
 So the string() inside @test sees an expression, but the string() outside 
the macro sees the evaluated result (an anonymous function). 

Remember macros evaluate at compile time, not runtime.

Cheers
Lex

Reply via email to