# Does declaring a variable to be constant do anything at all?

**URL:** <https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029>\
**Category:** General Usage\
**Tags:** question, constants\
**Created:** [September 21, 2017, 5:58pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029 "2017-09-21T17:58:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 21, 2017, 5:58pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/1 "2017-09-21T17:58:01Z")

</div>

The code

```
const x = 4
x = 3

```

issues a warning but still changes the value of `x`. And you can change bits-type fields of constant instances of mutable types without even getting a warning:

```
mutable struct Mut
  x::Int64
end

const myMut = Mut(5)
myMut.x = 6

```

What is the point of constant variables if they can be changed? Apparently declaring global variables to be constant can improve performance - but if they’re not actually constant, then are you getting that speedup for free?

---

<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:** [September 21, 2017, 6:03pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/2 "2017-09-21T18:03:43Z")

</div>

Declaring them constant fixes the type, which is what improves performance. This is enforced, eg

```julia
julia> const x = 42
42

julia> x = 2.71
ERROR: invalid redefinition of constant x

```

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [September 21, 2017, 6:14pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/3 "2017-09-21T18:14:49Z")

</div>

Besides the constant type which is useful for performance in the global scope, constant values do actually get inlined into functions. For example:

```julia
julia> const a = 5
5

julia> f(x) = a
f (generic function with 1 method)

julia> @code_llvm f(2.)

define i64 @julia_f_60745(double) #0 !dbg !5 {
top:
  ret i64 5
}

julia> a = 12
WARNING: redefining constant a
12

julia> @code_llvm f(3.)

define i64 @julia_f_60760(double) #0 !dbg !5 {
top:
  ret i64 5
}

```

Here we see that you are not getting the speedup for free. Changing the constant is allowed, but the function trusts that you won’t do this and doesn’t check the value again.

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 21, 2017, 6:39pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/5 "2017-09-21T18:39:01Z")

</div>

I see, thanks. As I mentioned above, there appears to be a slight inconsistency in the warning behavior triggered by changing constants: changing the value of a constant variable itself triggers a warning, but changing the value of a field (even a bits-type field) of a constant instance of a mutable composite type does not. Shouldn’t the latter action also trigger a warning, for consistency?

---

<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:** [September 21, 2017, 7:24pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/6 "2017-09-21T19:24:47Z")

</div>

No, the constness is on the binding, not the object being “pointed” at.

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 21, 2017, 8:08pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/7 "2017-09-21T20:08:29Z")

</div>

Am I correct in understanding the difference as being that reassigning a global variable writes the new value to a new memory location and then rebinds the variable to the new memory location, whereas reassigning a field of a composite type overwrites the new value in the same memory location as the old data, so that the field’s binding doesn’t change?

---

<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:** [September 21, 2017, 8:14pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/8 "2017-09-21T20:14:42Z")

</div>

No, the field binding can very well change. The constness is not on the field binding but of the binding from the variable to the object.

Perhaps this is useful reading: [Values vs. Bindings: The Map is Not the Territory · John Myles White](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/).

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 21, 2017, 9:49pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/9 "2017-09-21T21:49:09Z")

</div>

Oh, I see, when you said “the constness is on the binding” you were referring to the binding of the variable “myMut” to the particular object. I thought you were referring to the binding of the field “myMut.x” to 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:** [September 21, 2017, 10:48pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/11 "2017-09-21T22:48:53Z")

</div>

No the two concepts are orthogonal. You shouldn’t describe mutating an object as binding. I’m not sure what background you have (i.e. what languages are you familiar with) but I’ll try and point out that variables are orthorgonal to object in julia, just like most scripting languages. This is unlike C++ for example, where variables basically equal objects. Immutable means, well, you can’t mutate an object. Constant global means you can’t change the object it’s pointing to.

---

<div class="post-metadata">

**Author:** ![tparker](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tparker](https://discourse.julialang.org/u/tparker)\
**Post date:** [September 21, 2017, 11:07pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/13 "2017-09-21T23:07:02Z")

</div>

I understand, thanks.

---

<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:** [September 21, 2017, 11:07pm UTC](https://discourse.julialang.org/t/does-declaring-a-variable-to-be-constant-do-anything-at-all/6029/14 "2017-09-21T23:07:07Z")

</div>

No. immutable is unrelated to no-rebinding. I would have said you can rebind immutable just fine but this sentense itself would not make any sense.  
Basically type is not the property of a name (variable) and mutability is a property of the type so you can’t talk about something being immutable and having it being the subject of binding. Such a concept doesn’t exist.

To give examples. You can assign a immutable type to a variable and assign anything else to the same variable. The variable holding a immutable type currently does not affect what else you can put in it.

OTOH, (this you already mentioned) you can have a constant global whose value is a mutable type. You can’t change which object the variable points to but you can mutate the value just fine.
