Basic @macro facility With Struct


#1

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


#2

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


#3

(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.)


#5

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

#6

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


#7

I think it might be a misplaced parenthesis:

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