As I was navigating the forum about constant redefinition, I noticed that, as per 1.12 release notes:
Redefinition of constants is now well defined and follows world age semantics (#57253). Additional redefinitions (e.g. of types) are now allowed. See the new manual chapter on world age.
And indeed, at least in the interactive command line (REPL) of Julia 1.13, the following session “just works” (no warning):
julia> const a=1
1
julia> const a=2
2
Still, reading through the docstring of the const keyword, it states at the end:
In some cases[bold format is mine] changing the value of a const variable gives a warning instead of an error. However, this can produce unpredictable behavior or corrupt the state of your program, and so should be avoided. This feature is intended only for convenience during interactive use.
So my question is: which are these “some cases” where redefining a const gives a warning?
Or is this text an historical trace of the “pre Julia 1.12 era”?
So now a constant is a variable whose value is not initially expected to change, so that when you’re writing a module you get to worry about the side effects of modifying a constant in one routine mean you have to keep track of where it messes up another? I suppose it makes sense in terms of the traditional advice to avoid “magic numbers,” but I think it’s going to mess with peoples’ heads.
I believe this warning is a vestige that is no longer accurate.
When they say that const bindings are tied to world age, this means that changing a const will be correctly reflected in the next world age (see the example in the link, and explanation elsewhere on that page for when the world age advances). But you can count on that to happen correctly (or else it should be reported as a bug).
This also means that the price of changing a const variable is that it will usually force recompilation of any method instance that depends on it.
Still one thing isn’t clear from all this const/type redefinition feature: the Julia 1.12 highlights blog post show case the feature in the REPL. However, it doesn’t says if this feature is only available on the command line or in other context (e.g. in Modules).
It’s a language feature - there’s nothing REPL specific about it, although you may encounter unintuitive behavior in non-REPL contexts if you accidentally run code in an old world age. The details are documented in the mentioned manual chapter.
I guess you are refering to the manual chapter The World Age mechanism · The Julia Language, correct? If so, I think there is a documentation issue for explaining the nonconstantness of the const keyword, as the world age chapter starts with the following note
World age is an advanced concept. For the vast majority of Julia users, the world age mechanism operates invisibly in the background. This documentation is intended for the few users who may encounter world-age related issues or error messages.
which basically says to me: “as a new user, you don’t need to read this.”, while the “nonconstantness” of const may interest a wider audience. What do you think
/*
* If the new process paused because it was
* swapped out, set the stack level to the last call
* to savu(u_ssav). This means that the return
* which is executed immediately after the call to aretu
* actually returns from the last routine which did
* the savu.
*
* You are not expected to understand this.
*/
if(rp->p_flag&SSWAP) {
rp->p_flag =& ~SSWAP;
aretu(u.u_ssav);
}
A const variable is very-much-like any other variable except that it tells the compiler “whenever you see this variable, you can assume it will never change.” This is fine because if it does actually change the compiler will be re-run on everything that depended on it. The caveat is that it won’t be updated until the next world age.
Put differently, a Julia const variable is no more const than a C++ const (and not only because Julia const is nonrecursive). No one insists that a C++ const can’t change between builds. Since Julia compilation is an open-ended process, it can expose a useful concept of const redefinition.
Changing a const via the REPL or in a file tracked by Revise.jl will work like you would expect (at least by the time the world age advances, such as at the next top-level statement). You have to go out of your way to encounter hiccups with const redefinitions. Notice that every (?) example of “weird” behavior on the world age page relies on the combination of eval and a context that doesn’t advance the world age (e.g., begin).
That’s why this is considered an advanced topic. I’m not really aware of any non-abusive use of const redefinition that will encounter trouble.
I think for the vast majority of users, it just does what they expect. Not being able to redefine const was an exception to the usual operation of the language (e.g. redefining a method is the same operation). From the standpoint of the overall design of the language, I don’t think it needs to be called out as a special feature. I think the primary reason that it feels that was is just the historical behavior, but that will fade into the background in a few years.
Thanks for your feedback. I find your parallel with C++ interesting, but I’m less convinced from a simple user point of view:
for a fresh C++ user, the build process is quite obvious, as you have to press/call some “build” button/command
for Julia, the fact that it gets compiled in the background is an implementation detail, as it is fully automated
At the end, no matter the const behavior, a nice usage story to promote its usage is needed, otherwise, why should a user care? Something like:
any global parameter of your code that is not meant to change during code execution should be defined as const and this subtle change can dramatically improve execution speed
and actually there is something in this line in the Performance tips chapter:
We find that global names are frequently constants, and declaring them as such greatly improves performance: …
However, I find this sentence is a bit buried in the section which deals mostly with typing.
Looks like Essentials · The Julia Language. At the bottom-right of the box there’s a “source” button which takes you to basedocs.jl.
For what it’s worth, in many situations the performance benefit of const is also delivered by a typed global, which has constant type but not constant value. The main places where const provides an improvement is where it can move a nontrivial calculation or a variable type parameter (which would otherwise cause type instability) to a compile time constant. But Julia’s effects system is a little weak at proving this (rather, some things are more effect-full than you’d expect) so it might not happen every time you’d expect.