# Strange behavior, when constant is redefined inside a function

**URL:** <https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326>\
**Category:** General Usage\
**Created:** [June 17, 2017, 10:21am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326 "2017-06-17T10:21:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 10:21am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/1 "2017-06-17T10:21:35Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 10:34am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/2 "2017-06-17T10:34:11Z")

</div>

Ignoring warnings is typically a bad idea.

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 10:40am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/3 "2017-06-17T10:40:58Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 10:53am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/4 "2017-06-17T10:53:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 11:00am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/5 "2017-06-17T11:00:42Z")

</div>

> [@DrKrar](#):
>
> m.z

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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 2:34pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/6 "2017-06-17T14:34:01Z")

</div>

What is the question?

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 3:03pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/7 "2017-06-17T15:03:56Z")

</div>

> [@kristoffer.carlsson](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 4:55pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/8 "2017-06-17T16:55:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JaredCrean2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaredcrean2/32/3574_2.png) [@JaredCrean2](https://discourse.julialang.org/u/JaredCrean2)\
**Post date:** [June 17, 2017, 5:02pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/9 "2017-06-17T17:02:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 6:11pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/10 "2017-06-17T18:11:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 7:11pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/11 "2017-06-17T19:11:49Z")

</div>

> [@kristoffer.carlsson](#):
>
> 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.

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

i think this desire a bug report.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 17, 2017, 7:22pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/12 "2017-06-17T19:22:53Z")

</div>

> [@DrKrar](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [June 17, 2017, 7:34pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/13 "2017-06-17T19:34:46Z")

</div>

> [@yuyichao](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 17, 2017, 7:38pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/14 "2017-06-17T19:38:36Z")

</div>

> [@DrKrar](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 17, 2017, 8:20pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/15 "2017-06-17T20:20:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 17, 2017, 8:28pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/16 "2017-06-17T20:28:09Z")

</div>

> [@DrKrar](#):
>
> 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.

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

> [@PetrKryslUCSD](#):
>
> why is there no warning when redefining a const as m.z=5?

How do you do that?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 18, 2017, 12:03am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/17 "2017-06-18T00:03:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 18, 2017, 12:41am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/18 "2017-06-18T00:41:01Z")

</div>

> [@PetrKryslUCSD](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 18, 2017, 1:28am UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/19 "2017-06-18T01:28:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DrKrar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drkrar/32/3801_2.png) [@DrKrar](https://discourse.julialang.org/u/DrKrar)\
**Post date:** [August 14, 2017, 10:17pm UTC](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326/20 "2017-08-14T22:17:32Z")

</div>

const in a local scope is ignored.

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

[Next page](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326.md?page=2)
