# Confused about global variables

**URL:** <https://discourse.julialang.org/t/confused-about-global-variables/4441>\
**Category:** General Usage\
**Created:** [June 24, 2017, 4:10pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441 "2017-06-24T16:10:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimran/32/7782_2.png) [@kimran](https://discourse.julialang.org/u/kimran)\
**Post date:** [June 24, 2017, 4:10pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/1 "2017-06-24T16:10:09Z")

</div>

Consider the following code

```julia
module T

const a = true

function c_a(new_a)
 global a
 a = new_a
end

function p_a()
 println(a)
end

end

```

However, `T.p_a()` does not seem to pick up the changed `a` after running `T.c_a(false)`

```julia
julia> VERSION
v"0.6.0"

julia> T.a
true

julia> T.p_a()
true

julia> T.c_a(false)
WARNING: redefining constant a
false

julia> T.a
false

julia> T.p_a()
true

```

What am I doing wrong?  
(Just to be clear, I want to change the value of the global var a and use this in functions in the same module T)

Thanks

---

<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 24, 2017, 5:18pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/2 "2017-06-24T17:18:56Z")

</div>

See [Strange behavior, when constant is redefined inside a function](https://discourse.julialang.org/t/strange-behavior-when-constant-is-redefined-inside-a-function/4326)

![](https://global.discourse-cdn.com/julialang/original/3X/5/1/51e8a85d97e26a881fed1fd34acaaa95ada4e9cf.jpeg)

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 24, 2017, 9:17pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/3 "2017-06-24T21:17:15Z")

</div>

I think the problem is that the “performance tips” section of the manual tells people to declare global variables as `const` or they will be slow. When people then discover that the value of a `const` can be changed, they figure that they’ve found a way to have fast globals.

Better documentation might help, but a much cooler solution would be if constants were treated as inlined functions, so that redefining a constant triggers re-compilation of every function where it is used. (I don’t care if this is slow. Just add another performance tip saying “avoid redefining constants”.)

---

<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 24, 2017, 9:25pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/4 "2017-06-24T21:25:26Z")

</div>

> [@Per](#):
>
> if constants were treated as inlined functions, so that redefining a constant triggers re-compilation of every function where it is used.

Absolutely not.

---

<div class="post-metadata">

**Author:** ![hustf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hustf/32/2374_2.png) [@hustf](https://discourse.julialang.org/u/hustf)\
**Post date:** [June 24, 2017, 10:42pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/5 "2017-06-24T22:42:59Z")

</div>

The code below gives the expected behaviour.

A language I daren’t mention here calls variables with this behaviour ‘Private’, not ‘Global’.

For package quality code, this way of doing things is perhaps sub-optimal. But for everyday code, this is a great way of doing things.

```julia
module T

global a = true

function c_a(new_a)
 global a
 a = new_a
end

function p_a()
 println(a)
end

end

```

---

<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 25, 2017, 5:22am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/6 "2017-06-25T05:22:19Z")

</div>

What is a good use case for global variables? Is there an argument that can be made for their inevitability?  
Or, can global variables be avoided altogether?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 25, 2017, 6:47am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/7 "2017-06-25T06:47:24Z")

</div>

> [@Per](#):
>
> Better documentation might help

It is [already there](https://docs.julialang.org/en/latest/manual/variables-and-scoping/#Constants-1):

> Note that const only affects the variable binding; the variable may be bound to a mutable object (such as an array), and that object may still be modified.

so eg

```julia
module T

const a = [true]
change_flag(flag::Bool) = a[1] = flag
get_flag() = a[1]

end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 25, 2017, 8:05am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/8 "2017-06-25T08:05:56Z")

</div>

Functions are globals

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 25, 2017, 9:04am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/9 "2017-06-25T09:04:44Z")

</div>

> [@ChrisRackauckas](#):
>
> Functions are globals

Yes, but they are not, strictly speaking, global _variables_. They behave like something halfway between a variable and a `const`. Compare the following examples:

```julia
a = 0
function f()
    global a = 1
    println(a)
end

b() = 0
function g()
    global b() = 1
    println(b())
end

const c = 0
function h()
    global c = 1
    println(c)
end

```

The first invocation of `f()` prints `1`, but the first invocations of `g()` and `h()` print `0`. Still, `g()` prints `1` the second time, while `h()` prints `0` every time.

If it were up to me, I’d forbid redefining both functions and global `const`s from inside a function. If someone really wants to do it, he should be forced to use an `eval` to drop down to the global scope.

---

<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 25, 2017, 9:17am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/10 "2017-06-25T09:17:57Z")

</div>

> [@Per](#):
>
> They behave like something halfway between a variable and a const. Compare the following examples:

No, they are even more restricted than global constants. What you are doing in your code is mutation and not changing the binding at all.

---

<div class="post-metadata">

**Author:** ![kimran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimran/32/7782_2.png) [@kimran](https://discourse.julialang.org/u/kimran)\
**Post date:** [June 26, 2017, 6:29pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/11 "2017-06-26T18:29:35Z")

</div>

So the confusion for me was that I thought `const` fixes the type of the variable and that cannot be changed, but the value can be changed

Indeed `T.a` did change its value

But `T.p_a()` was compiled and **cached** and hence assumed `T.a` to not change value

Correct me if my understanding is wrong

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 26, 2017, 7:06pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/12 "2017-06-26T19:06:04Z")

</div>

For what it’s worth, using `const` global _containers_ and treating them as you would any other normal container is something I occasionally resort to, and it always seems to work just fine (perhaps the devs will have further commentary on whether this is really a smart thing to do). In my experience, there are some (rare) instances when it is simply far more convenient to associate certain variables with a module than to force users to have to juggle objects which manage them. Otherwise, I’m not too sure why this issue keeps coming up: why would you assume that redefining something that you explicitly declared to be a `const` would result in “normal” behavior?

---

<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 26, 2017, 9:09pm UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/13 "2017-06-26T21:09:13Z")

</div>

> [@kimran](#):
>
> const fixes the type of the variable and that cannot be changed, but the value can be changed

The value cannot be changed without a warning telling you that things shouldn’t crash but can start to be inconsistent. What inconsistency you might see is only an implementation detail.

> [@ExpandingMan](#):
>
> const global containers

If you really need something that doesn’t change type, use a global const `Ref`.

---

<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 27, 2017, 6:55am UTC](https://discourse.julialang.org/t/confused-about-global-variables/4441/14 "2017-06-27T06:55:11Z")

</div>

> [@kimran](#):
>
> a after running T.c\_a(false)

I think this is a great solution.
