Basic @macro facility With Struct

back to first steps for me, this type with macros. This should be trivially easy:

mutable struct Point; x::Float64; f::Float64; end#struct
p= Point( 1.0, 2.0 )
macro testme( newp );  (newp.f <= 0.0) ? "$newp below" : "$newp above"; end#macro
@testme( p )

I tried a couple of variations on newp in the macro. Futile. How do I tell the macro processor to replace newp with p upon invokation?

regards, /iaw

I’m not sure what you’re trying to do, but why not make testme a normal function?

(doing this because it is an illustrative example with minimal features. ultimately, I want the macro to return from the function, not from the macro.)

Sorry, I might have misunderstood.
Is this what you want?

macro testme(newp)
    esc(:($newp.f <= 0.0 ? string($newp, " below") : string($newp, " above")))
end

You can see what expression the macro returns with macroexpand:

Julia-0.6.2> macroexpand(quote @testme p end)
quote  # REPL[6], line 1:
    if p.f <= 0.0
        string(p, " below")
    else
        string(p, " above")
    end
end
2 Likes

exactly. thank you, greg. alas, simple generalization, which usually works well for me, is a bit more difficult than I thought. Can I beg for an expanded example?

 macro test( newp )
               esc(:($newp.f <= Inf) && return $newp)
       end

is not cooperating.

regards,

/iaw

I think it might be a misplaced parenthesis:

macro test(newp)
    esc(:($newp.f <= Inf && return $newp))
end
1 Like