# @assert: what is the proper tool?

**URL:** <https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217>\
**Category:** General Usage\
**Created:** [March 19, 2020, 6:37pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217 "2020-03-19T18:37:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 19, 2020, 6:37pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/1 "2020-03-19T18:37:59Z")

</div>

Apparently `@assert` can be elided by the compiler for some optimization level.  
What is the recommended easy and fool-proof ways of asserting program behaviours?

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [March 19, 2020, 6:50pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/2 "2020-03-19T18:50:27Z")

</div>

```julia
if !(condition)
 error("error")
end

```

You can use `@test` too, but previous one is better.

```julia
using Test
@test condition

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 19, 2020, 6:52pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/3 "2020-03-19T18:52:43Z")

</div>

Yes, that is possible. But `@assert condition "Try again!"` is so much nicer.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [March 19, 2020, 6:55pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/4 "2020-03-19T18:55:05Z")

</div>

```julia
macro myassert(ex, msgs...)
    msg = isempty(msgs) ? ex : msgs[1]
    if isa(msg, AbstractString)
        msg = msg # pass-through
    elseif !isempty(msgs) && (isa(msg, Expr) || isa(msg, Symbol))
        # message is an expression needing evaluating
        msg = :(Main.Base.string($(esc(msg))))
    elseif isdefined(Main, :Base) && isdefined(Main.Base, :string) && applicable(Main.Base.string, msg)
        msg = Main.Base.string(msg)
    else
        # string() might not be defined during bootstrap
        msg = quote
            msg = $(Expr(:quote,msg))
            isdefined(Main, :Base) ? Main.Base.string(msg) :
                (Core.println(msg); "Error during bootstrap. See stdout.")
        end
    end
    return :($(esc(ex)) ? $(nothing) : error($msg))
end

```

```julia
@myassert condition "try again!"

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 19, 2020, 6:57pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/5 "2020-03-19T18:57:03Z")

</div>

Nice. But still: this means having to define this in all of your packages (or having one and using it).  
Isn’t there something standard?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 19, 2020, 7:03pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/6 "2020-03-19T19:03:55Z")

</div>

[GitHub - jw3126/ArgCheck.jl: Package for checking function arguments](https://github.com/jw3126/ArgCheck.jl) has `@check` macro

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [February 6, 2024, 5:47pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/7 "2024-02-06T17:47:59Z")

</div>

Just to confirm, If I don’t want to deal with packages, and want a macro that does exactly what `@assert` does, I should take the definition of `@assert` and rename the macro to something like `@myassert`, as Amin\_Yahyaabadi did, right? The compiler will not turn it off, right?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 6, 2024, 6:00pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/8 "2024-02-06T18:00:15Z")

</div>

Correct.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 6, 2024, 6:11pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/9 "2024-02-06T18:11:38Z")

</div>

> [@sadish-d](#):
>
> … I should take the definition of `@assert` and rename the macro to something like `@myassert` …

Not quite. The last line of the `@assert` macro reads

```julia
return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg)))

```

The last line of `@myassert` provided above in this thread is

```julia
return :($(esc(ex)) ? $(nothing) : error($msg))

```

This ensures that if the compiler chooses to ignore `AssertionError`s, the `@myassert` macro will still throw on a `false` value of its first argument.

---

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [February 6, 2024, 6:38pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/10 "2024-02-06T18:38:00Z")

</div>

Thanks for pointing out the distinction.

The documentation of `@assert` gives a warning that the compiler might “disable” assertions. But you’re implying that this is a warning specifically about `AssertionError` rather than anything else in the macro itself. Is that correct?

If so, it would be quite important to include that warning in the definition of `AssertionError` itself I think. I’m on Julia 1.9, and `AssertionError` has no such warnings.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 6, 2024, 7:18pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/11 "2024-02-06T19:18:04Z")

</div>

> [@sadish-d](#):
>
> this is a warning specifically about `AssertionError` rather than anything else in the macro itself. Is that correct?

I believe that is correct. Regarding adding the warning to the documentation for `AssertionError`: To my knowledge, `AssertionError` isn’t really thrown in practice except by the `@assert` macro, for which it is intended. So it’s really enough to document the macro, which is what users are supposed to invoke to generate this flavor of `Exception`.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 6, 2024, 7:53pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/12 "2024-02-06T19:53:10Z")

</div>

The way the docs read to me is that the compiler could decide to elide `@assert`, not that it could decide to ignore `AssertionError`. Am I wrong?

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 6, 2024, 9:48pm UTC](https://discourse.julialang.org/t/assert-what-is-the-proper-tool/36217/13 "2024-02-06T21:48:21Z")

</div>

You’re right. I don’t really know how this would be implemented. And after thinking about it, it’s probably easier to test the optimization level inside the macro logic, prior to macro expansion to achieve zero run-time cost.
