# @assert alternatives?

**URL:** <https://discourse.julialang.org/t/assert-alternatives/24775>\
**Category:** New to Julia\
**Created:** [May 30, 2019, 5:22pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775 "2019-05-30T17:22:15Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 31, 2019, 6:21pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775/21 "2019-05-31T18:21:24Z")

</div>

> [@Henrique\_Becker](#):
>
> I think this is a unavoidable downside.

Yes, from a users perspective in current julia yes. I was talking about it from the point of view that the `@assert` macro in Base would work like this. For this cases I think it would be better to have a startup option with two different sysimg and precompilation directories. The downside being that you can’t toggle it in a session.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 31, 2019, 6:41pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775/22 "2019-05-31T18:41:31Z")

</div>

> For this cases I think it would be better to have a startup option with two different sysimg and precompilation directories.

Even the bit-polishing performance-obsessed me is thinking this could  
be a bit of an overkill for `@assert`, XD. If we grouped all training  
wheels and/or non-essentials (`@assert`, bound checking, TimerOutputs,  
etc…), then maybe having this infrastructure to run the code with or  
without them would be justified. Maybe.

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [September 12, 2022, 7:53am UTC](https://discourse.julialang.org/t/assert-alternatives/24775/23 "2022-09-12T07:53:39Z")

</div>

Is ToggleableAsserts.jl still the state-of-the-art to use assertions in high-performance-code?

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 12, 2022, 9:45pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775/24 "2022-09-12T21:45:52Z")

</div>

Check out [`ArgCheck.jl`](https://github.com/jw3126/ArgCheck.jl) and [`OptionalArgChecks.jl`](https://github.com/simeonschaub/OptionalArgChecks.jl).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 13, 2022, 12:12am UTC](https://discourse.julialang.org/t/assert-alternatives/24775/25 "2022-09-13T00:12:16Z")

</div>

Very interesting. If I am in need of this kind of check again, I will  
maybe try to update the package to make them work again.

---

<div class="post-metadata">

**Author:** ![dpinol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpinol/32/26550_2.png) [@dpinol](https://discourse.julialang.org/u/dpinol)\
**Post date:** [February 27, 2023, 2:47pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775/26 "2023-02-27T14:47:24Z")

</div>

Great solution.  
I ended up adding something like the following to my module’s ` __init__ ()` function:

```julia
@eval asserting() = get(ENV, "ENABLE_ASSERTS", "0") == "1" 

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 27, 2023, 2:55pm UTC](https://discourse.julialang.org/t/assert-alternatives/24775/27 "2023-02-27T14:55:56Z")

</div>

Why does that need to be in the ` __init__ ` function (and use `@eval` 😱)?

---

<div class="post-metadata">

**Author:** ![dpinol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpinol/32/26550_2.png) [@dpinol](https://discourse.julialang.org/u/dpinol)\
**Post date:** [November 25, 2024, 11:38am UTC](https://discourse.julialang.org/t/assert-alternatives/24775/28 "2024-11-25T11:38:10Z")

</div>

Well, I put it in **init** to get the value of the env var at runtime, and not at precompilation. And @eval is necessary when you redefine a function from inside another function.

But I eventually discarded this solution because I randomly got this error:

ERROR: LoadError: InitError: Evaluation into the closed module `MyModule` breaks incremental compilation because the side effects will not be permanent. This is likely due to some other module mutating `SCEngine` with `eval` during precompilation - don’t do this.

[Previous page](https://discourse.julialang.org/t/assert-alternatives/24775.md?page=1)
