Cannot declare Main.foo constant; it already has a value

I experiment with setting the value of foo, decide I’m happy with it, and I want to make it const but I can’t. Can something be done to make this less annoying?

julia> foo = []
Any[]

julia> const foo = []
ERROR: cannot declare Main.foo constant; it already has a value

I proposed a similar mechanism recently, but the problem is that functions may have been compiled previously that assume that the binding is mutable. We don’t track those kinds of edges currently, so it is not possible to make a binding const after the fact. It’d need something like the world-age dependent bindings table that I proposed for the struct redefinition problem.

9 Likes

Should we downgrade this to a warning? It is not like the code can be caused to misbehave by failing to convert it to a const binding; so it is safe to ignore that, even if the user might find that to be incongruous of it.

Actually, you can cause misbehavior by failing to convert it to a const binding.

After declaring without error that const foo = [], what should the following code return: isconst(Main, :foo)? Obviously, if you are only referring to using the value of foo then you cannot cause any misbehavior, but there are other behaviors that might go past that (even if those are not so frequent).

That is probably undesirable behavior by the user, but it doesn’t exhibit any significant misbehavior for the compiler