Strange behavior, when constant is redefined inside a function


#1

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.


Confused about global variables
#2

Ignoring warnings is typically a bad idea.


#3

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.


#4

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.


#5

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


#6

What is the question?


#7

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


#8

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


#9

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.


#10

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


#11

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

i think this desire a bug report.


#12

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.


#13

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.


#14

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.


#15

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


#16

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?


#17

@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)?


#18

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.


#19

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


#20

const in a local scope is ignored.