# Why can't I combine a macro call with a comparison operator?

**URL:** https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493
**Category:** General Usage
**Tags:** macros, parsing, operator
**Created:** [January 4, 2023, 10:00am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493 "2023-01-04T10:00:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 4, 2023, 10:00am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/1 "2023-01-04T10:00:14Z")

</div>

To save myself a bit of typing, I have created a wrapper macro around a function in my model, to be used in a specific context:

```julia
macro landcover()
    :(landcover($(esc(:pos)), $(esc(:model))))
end

```

I want to be able to use this macro in conditional statements. However, doing so throws an error:

```julia
julia> @macroexpand @landcover
:(MyModel.landcover(pos, model))

julia> landcover(pos, model) == 20
true

julia> @landcover == 20
ERROR: syntax: "==" is not a unary operator
Stacktrace:
 [1] top-level scope
   @ none:1

```

Why is that?

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 4, 2023, 10:12am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/2 "2023-01-04T10:12:31Z")

</div>

You need parentheses to keep the macro call from slurping up all the expressions to the right of it.

```julia
@landcover() == 20

```

or

```julia
(@landcover) == 20

```

or

```julia
20 == @landcover

```

See [docs](https://docs.julialang.org/en/v1/manual/metaprogramming/#Macro-invocation).

---

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 4, 2023, 11:12am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/3 "2023-01-04T11:12:40Z")

</div>

True, I should have thought of that… My expectation was that since the macro doesn’t take any arguments, any expressions that followed its invocation would therefore not be considered possible arguments.

---

<div class="post-metadata">

### Author: ![veddox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veddox/32/45227_2.png) [@veddox](https://discourse.julialang.org/u/veddox)
#### Post date: [January 4, 2023, 11:19am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/4 "2023-01-04T11:19:11Z")

</div>

I take it that Julia has no equivalent to Common Lisp’s symbol macros / parameterless macros?

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [January 4, 2023, 11:44am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/5 "2023-01-04T11:44:29Z")

</div>

Julia has multiple dispatch for macros too, so you can define more macros with the same name that do take those arguments. That can’t be known at parse time, so what you want isn’t possible.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 5, 2023, 6:13am UTC](https://discourse.julialang.org/t/why-cant-i-combine-a-macro-call-with-a-comparison-operator/92493/6 "2023-01-05T06:13:37Z")

</div>

To illustrate that macros are really just a special case of functions:

```julia
julia> function var"@foo"(_, _, arg) QuoteNode(arg) end
@foo (macro with 1 method)

julia> @foo Hello, world!
:((Hello, world!))

```

Edit: Note that this is not official API, and should probably not be relied upon in case it’s ever decided to prepend additional arguments into macro invocations.
