# Basic @macro facility With Struct

**URL:** <https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874>\
**Category:** New to Julia\
**Created:** [May 13, 2018, 10:50pm UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874 "2018-05-13T22:50:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 13, 2018, 10:50pm UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/1 "2018-05-13T22:50:21Z")

</div>

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

```julia
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

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 13, 2018, 11:02pm UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/2 "2018-05-13T23:02:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 13, 2018, 11:04pm UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/3 "2018-05-13T23:04:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 13, 2018, 11:27pm UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/5 "2018-05-13T23:27:06Z")

</div>

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

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [May 14, 2018, 12:28am UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/6 "2018-05-14T00:28:33Z")

</div>

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?

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

```

is not cooperating.

regards,

/iaw

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 14, 2018, 1:07am UTC](https://discourse.julialang.org/t/basic-macro-facility-with-struct/10874/7 "2018-05-14T01:07:14Z")

</div>

I think it might be a misplaced parenthesis:

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