What is correct form in this case?

The exact same problem again: you need to *quote* the returned expression 
from the macro, to make sure that the code is run when the macro is called, 
and not when it’s defined.

This will work as intended:

macro tst(i::Int)
    quote
        tst3(i)
    end
end

# or

macro tst(i::Int)
    :(tst3(i))
end

This section of the manual 
<http://docs.julialang.org/en/release-0.4/manual/metaprogramming/> might be 
worth reading (again) :)

// T

On Monday, September 28, 2015 at 7:20:03 AM UTC+2, Leonardo wrote:

Many thanks!
>
> I have a similar problem calling a function in following scenario:
> function tst3(i::Int)
>     println(i)
> end
>
> macro tst(i::Int)
>     tst3(i)
> end
>
> I obtain an error executing code:
> julia> b = 2::Int
> julia> @tst b
> complaining for a problem of type, because tst3 seems to receive a Symbol 
> instead an Integer:
> ERROR: TypeError: anonymous: in typeassert, expected Int64, got Symbol
>
> What is correct form in this case?
>
> Leonardo
>
>
> Il 27/09/2015 18:58, Yichao Yu ha scritto:
>
> On Sun, Sep 27, 2015 at 12:20 PM, Leonardo <max...@gmail.com> <javascript:> 
> wrote:
>
> Hi all,
> I need manipulate AST of an expression in a macro, and pass same expression
> in input to another macro, but I experience some problem.
>
> I try to express my doubt with a simplified example:
> macro tst2(e2::Expr)
>     println(e2.head)
> end
>
> macro tst(e1::Expr)
>     @tst2 esc(e1)
> end
>
> ```
> macro tst(e1::Expr)
>     quote
>         @tst2 $(esc(e1))
>     end
> end
> ```
>
> otherwise, `@tst2` will be run at `tst` definition time.
>
>
> In previous code I want that call to @tst2 behave exact like call to @tst.
>
> E.g. calling @tst2 in REPL I obtain:
> julia> a = 2
> julia> @tst2(a < 3)
> comparison
> but same call to @tst has no effect, and I cannot find an alternative
> working form for @tst
>
> Someone can help me to understand this odd behaviour of Julia's macro?
>
> Many thanks in advance
>
> Leonardo
>
>
>
> ​

Reply via email to