In your two examples, note the difference *when* the macro argument is 
multiplied by 2.

@ex_timestwo happens at runtime
@n_timestwo happens at macro expansion time

a = 6
@ex_timestwo(a)
will return 12 as expected,

whereas:
a = 6
@n_timestwo(a)
will still result in error because at macro expansion time, it is trying to 
evaluate (:a) * 2



On Saturday, October 1, 2016 at 7:26:50 PM UTC+10, Lyndon White wrote:

> I was generally under the impression that a macro always had the return a 
> `Expr`
> and that doing otherwise would make the compiler yell at me.
>
> Apparently I was wrong, at least about the second part:
> https://github.com/ChrisRackauckas/ParallelDataTransfer.jl/issues/3
>
>
>
> macro ex_timestwo(x)
>     :($(esc(x))*2)
> end
>
>
>
> @ex_timestwo(4)
> 8
>
> macroexpand(:(@ex_timestwo(4)))
> :(4 * 2)
>
> @ex_timestwo(a)
> LoadError: UndefVarError: a not defined
>
>
> macroexpand(:(@ex_timestwo(a)))
> :(a * 2)
>
>
>
> ?
>
> VS: not returning an expression, and just doing a thing:
>
> macro n_timestwo(x)
>  x*2
> end
>
>
> @n_timestwo(4)
> 8
>
>
> macroexpand(:(@n_timestwo(4)))
> 8
>
>
> @n_timestwo(a)
> LoadError: MethodError: no method matching *(::Symbol, ::Int64)
>
>
> macroexpand(:(@n_timestwo(a)))
> :($(Expr(:error, MethodError(*,(:a,2)))))
>
>
>
>
> Is this a feature?
> Or is it just undefined behavior?
>
>

Reply via email to