Silence `const` re-definition warning

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

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> const x = 2
2

julia> @constredef x = 3  # no warning
3
1 Like

There is
https://juliaobserver.com/packages/Suppressor

2 Likes

I use this macro:

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

that gives:

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.

This is silent because you don’t change x.

3 Likes

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).

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).

1 Like

Recently there was a lengthy discussion about if redefining constants is undefined behavior or not, 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.

6 Likes

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.

10 Likes