On Sat, Oct 1, 2016 at 5:26 AM, Lyndon White <[email protected]> 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?
>

You can return whatever you want. If it is one of the AST node
type[1], it'll be used as a expression. Otherwise, it'll be used as a
literal value. This is because not all quotes/AST nodes are Expr
(`typeof(:(1))`) and you can use `QuoteNde`[2] if you want to embed an
arbitrary object literal.

[1] http://julia.readthedocs.io/en/latest/devdocs/ast/
[2] https://github.com/JuliaLang/julia/pull/18660

Reply via email to