Strange behavior, when constant is redefined inside a function

julia> module m
const z=-1
export f
function f(x)
global const z=x
println(z)
println(m.z)
end
end
m

julia> using m

if we try to call f it response in a dual way. while give warning but its value accessed from the module m did not changed ???

julia> f(5)
WARNING: redefining constant z
-1
-1

but if we acced it directly from outside m we get ???

julia> m.z
5

I thing this is a bug.

Ignoring warnings is typically a bad idea.

Ignoring warnings is typically a bad idea.

this is correct.
but the issue here the strange behavior (which is allowed thought with warning).

so, in your opinion that a warning is issued so you are in your own , or should it be a bug report specially we did not know from where such behavior originate.

If you lie by saying something is const and then modifying it then bad stuff can happen (and you get a warning). Why this is allowed at all is for convenience when working in the REPL.

1 Like

even if we are not able to tell the value of m.z.

What is the question?

why the same variable can have two values?
if this is appropriate and not a bug , i would like to know how?

The function assumed you where truthful when you said that z was a const so it “cached” that value when the function was compiled.

I’d argue that when the compiler detects the code is doing something that will produce incorrect/irrational behavior, it should be an error not a warning.

2 Likes

That would be pretty annoying for interactive use. Would be the same as erroring on function redefinition before #265 was fixed.

OK, but if the new and old definition both are available from the same variable??

i think this desire a bug report.

I don’t see what’s gained by making a warning into an error. The “strange” behavior will not cause the runtime to behave in any strange way by itself so this is very different from other cases where we throw an error since there’s no way we can proceed. In general, the warning are not printing to be ignored.

4 Likes

the warning tell the user take care the value of a constant changed.
but it is a lie the constant have two values old and new. i think it should change the value from old value to the new one.

That’s not possible to do (at least AFAIU not trivially). The choice is to either change the warning to be more descriptive or make it an error to redefine a constant.

I wonder: why is there no warning when redefining a const as m.z=5?

Well, the user lied first (the const declaration isn’t a constant) and so this is the best possible response.

How do you do that?

@yuyichao
It probably isn’t easy to do, but then again Julia already has immutable types. How about converting the type of the constant quantity to an immutable type (perhaps behind the scenes)?

I’m not exactly sure what you are suggesting but it’s already an immutable type in this case. FWIW, turning that warning into an error is trivial, just that it’s unclear what it helps. For people doing interactive work it allows the code to work with the caveat that the function using them should be redefined for the effect to be seen consistently. For people doing this by accident, the warning should be a clear enough signal. For the runtime itself, this won’t cause any crashing so it is OK to keep running.

Sorry, I forgot that it was impossible to change the constant quantity from the outside of the module already.
Ignore my comments please.

const in a local scope is ignored.

https://github.com/JuliaLang/julia/blob/5ed2f45df73f97c4cb6dbe8a0595a5f1d1e739a3/doc/src/manual/variables-and-scoping.md#constants