# Can expressions containing syntax errors be used in macro calls?

**URL:** <https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949>\
**Category:** General Usage\
**Tags:** macros, error, syntax\
**Created:** [September 17, 2023, 12:15pm UTC](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949 "2023-09-17T12:15:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [September 17, 2023, 12:15pm UTC](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949/1 "2023-09-17T12:15:32Z")

</div>

I’d like to use an expression like this one in a macro call

```julia
struct A(B)
    x::Int
end

```

which gives a syntax error when called without the macro wrap:

```julia
julia> struct A(B)
           x::Int
       end
ERROR: syntax: invalid type signature around REPL[3]:1
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

but it works inside a macro:

```julia
julia> macro a(s) end

julia> @a struct A(B)
              x::Int
          end

```

here I’m using Julia 1.9, can I expect in this case that no syntax error will be thrown in some new Julia 1.x version?

This question came up to my mind since for example this other expression gives a syntax error even when called from a macro:

```julia
julia> @a begin
             const x::Int 
          end
ERROR: syntax: expected assignment after "const"
Stacktrace:
 [1] top-level scope
   @ none:1

```

So, more generally, when is it possible to use expressions with syntax errors in macro calls? Should this be avoided even if the sligthly tweaked syntax seems better?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 17, 2023, 1:49pm UTC](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949/2 "2023-09-17T13:49:13Z")

</div>

As macros get parsed expressions, they should work on any syntax that does not raise a parse error:

```julia
julia> Meta.parse("""struct A(B)
           x::Int
       end""")
:(struct A(B)
      #= none:2 =#
      x::Int
  end)
julia> Meta.parse("""begin
           const x::Int
       end""")
ERROR: Base.Meta.ParseError("expected assignment after \"const\"")

```

If you really want to support currently illegal syntax (as defined by the parser), you would need a string macro and do the parsing yourself.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [September 17, 2023, 1:57pm UTC](https://discourse.julialang.org/t/can-expressions-containing-syntax-errors-be-used-in-macro-calls/103949/3 "2023-09-17T13:57:59Z")

</div>

Didn’t expect that the answer was that simple and “well-behaved”, thank you @bertschi
