# Silence \`const\` re-definition warning

**URL:** https://discourse.julialang.org/t/silence-const-re-definition-warning/51095
**Category:** General Usage
**Tags:** question
**Created:** [December 2, 2020, 9:34am UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095 "2020-12-02T09:34:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 2, 2020, 9:34am UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/1 "2020-12-02T09:34:24Z")

</div>

Is there a way to silence the const re-definition warning?

```julia
julia> const x = 2
2

julia> x = 3
WARNING: redefinition of constant x. This may fail, cause incorrect answers, or produce other errors.
3

```

Maybe a macro, like:

```julia
julia> const x = 2
2

julia> @constredef x = 3 # no warning
3

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 2, 2020, 10:02am UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/2 "2020-12-02T10:02:52Z")

</div>

There is  
[https://juliaobserver.com/packages/Suppressor](https://juliaobserver.com/packages/Suppressor)

---

<div class="post-metadata">

### Author: ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)
#### Post date: [December 2, 2020, 10:19am UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/3 "2020-12-02T10:19:12Z")

</div>

I use this macro:

```julia
macro quiet(ex) :(try $ex catch e end) end

```

that gives:

```julia
julia> const x=2
2

julia> x=3
WARNING: redefinition of constant x. This may fail, cause incorrect answers, or produce other errors.
3

julia> @quiet x=1
1

julia> 

```

I wrote this macro a few days ago, and registered it in my `startup.jl`, for another usage: sometimes I run a gaming program, highly recursive, and I wish interrupting it (CTRL/C) without displaying a long stacktrace of recursive calls.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 2, 2020, 10:21am UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/4 "2020-12-02T10:21:00Z")

</div>

This is silent because you don’t change `x`.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [December 2, 2020, 1:37pm UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/5 "2020-12-02T13:37:38Z")

</div>

Rather than “suppressing” it, I was hoping for a way to avoid emitting the warning in the first place (which I guess would be faster).

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 2, 2020, 1:46pm UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/6 "2020-12-02T13:46:19Z")

</div>

Perhaps you should explain why do you want to redefine constants, particularly since you seem to want to be doing that many times, as you are worried about speed. (probably it is not a good idea and there are better solutions to what you want to do).

---

<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: [December 2, 2020, 1:50pm UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/7 "2020-12-02T13:50:34Z")

</div>

Recently there was [a lengthy discussion about if redefining constants is undefined behavior or not](https://discourse.julialang.org/t/will-julia-ever-fix-its-using-performance-problems/50659/91), and the takeaway is that, at least for now, the Julia devs consider it to be undefined behavior. I do not really understand why are you worried about suppressing this message if your program has absolutely no guarantees of working as intended after this happens.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [December 2, 2020, 2:00pm UTC](https://discourse.julialang.org/t/silence-const-re-definition-warning/51095/8 "2020-12-02T14:00:45Z")

</div>

You should never be redefining constants in a final working program. If you do, all bets are off and your program might do arbitrarily badly behaved things. It probably won’t, but it might.
