Great discussion, which added a lot of nuances and explicit advice based on use cases.
The difficulty of expressing this so clearly might suggest that the keyword const is a misnomer, though it helps the Julia compiler optimize for the types of globals so defined.
Two suggestions:
-
Rename the keyword
const
totypeconst
. It’s a weird non-word but it makes the purpose clear and is odd enough not to be confused with any notion of immutable. -
Introduce a new keyword
immutable
that can be used as a modifier for variable initialization. This is a new feature unrelated to type stability of globals (although it provides that, too). For exampleimmutable: a = 5
. We don’t need to say ::Int32 (or whatever) because type inference will handle that in this case. This would define a true immutable (variable binding and value) similar to what one can do in other languages. The purpose is to create an understandable (hopefully) alias for some important constant so that it can be used throughout source code.
Example:
immutable: scaling_factor = 1.37e-3
Later, in the code
force = g * scaling_factor # yes, this is bogus...
That’s clearer than putting the float literal in the code. The compiler would need to enforce this and raise errors on any attempt to set a new binding for scaling_factor
.
Certainly neither is a high priority but could add some clarity for type constant globals and add a feature for folks (like me) who originally misused const as an immutable.