# Using @assert in another macro

**URL:** <https://discourse.julialang.org/t/using-assert-in-another-macro/134990>\
**Category:** General Usage\
**Created:** [January 11, 2026, 11:53am UTC](https://discourse.julialang.org/t/using-assert-in-another-macro/134990 "2026-01-11T11:53:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![densmojd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/densmojd/32/206891_2.png) [@densmojd](https://discourse.julialang.org/u/densmojd)\
**Post date:** [January 11, 2026, 11:53am UTC](https://discourse.julialang.org/t/using-assert-in-another-macro/134990/1 "2026-01-11T11:53:03Z")

</div>

I’m attempting to use `@assert` in the definition of another macro, and I cannot get the message associated with the `AssertionError` to be formed correctly. Here’s an MWE:

```julia
macro my_assert(arg)
    return :(@assert $(esc(arg)))
end

@assert false # returns ERROR: AssertionError: false
@my_assert false # returns ERROR: AssertionError: $(Expr(:escape, false))

```

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [January 11, 2026, 1:45pm UTC](https://discourse.julialang.org/t/using-assert-in-another-macro/134990/2 "2026-01-11T13:45:52Z")

</div>

This happens because `esc(x)` is represented explicitly in the AST (Abstract Syntax Tree) as `Expr(:escape, x)`. In your wrapper macro you pass `esc(arg)` as an argument to another macro (`@assert`), so `@assert` ends up seeing the escape node and prints it in the AssertionError.

Minimal fix: don’t escape the argument, escape the whole returned expression so the expander removes `:escape` before `@assert` is processed.

```julia
macro my_assert(arg)
    esc(:(@assert $arg))
end

```

Fully aligned with `@assert` (supports `@assert <cond> <msg>`):

```julia
macro my_assert(args...)
    esc(:(@assert $(args...)))
end

```

```julia-auto
julia> @my_assert false "🚫🚫🚫"
ERROR: AssertionError: 🚫🚫🚫

```

---

<div class="post-metadata">

**Author:** ![densmojd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/densmojd/32/206891_2.png) [@densmojd](https://discourse.julialang.org/u/densmojd)\
**Post date:** [January 11, 2026, 7:19pm UTC](https://discourse.julialang.org/t/using-assert-in-another-macro/134990/3 "2026-01-11T19:19:19Z")

</div>

That’s perfect. Thank you!
