The Meaning of `const`

878 views
Skip to first unread message

Leah Hanson

unread,
Jul 8, 2014, 11:34:10 PM7/8/14
to julia...@googlegroups.com
The only reference I can find in the manual to the `const` keyword is in the performance section [1]. Well, that and the `isconst` function[2].

This seems like it would be worth documenting, especially since it behaves significantly differently than the const that I remember from Java or C++, which prevented you from changing the value of const variable.

I think I understand now, that `const` in Julia means that the type of the variable must remain constant. This was very surprising to me.

~~~.jl
julia> const x = 10
10

julia> x += 1
Warning: redefining constant x
11

julia> x
11

julia> const arr = Int[1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> arr[1] = 13
13

julia> arr
3-element Array{Int64,1}:
 13
  2
  3

julia> x += 0.5
ERROR: invalid redefinition of constant x
~~~

I'd be happy to add this to the manual, but I'm not sure where it belongs. Any suggestions?


Tim Holy

unread,
Jul 9, 2014, 5:23:11 AM7/9/14
to julia...@googlegroups.com
Interesting question. Perhaps an example like this:

julia> const x = 10
10

julia> x = 11
Warning: redefining constant x
11

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

could come fairly early (maybe in "Variables"). I'd say mention of the array
behavior could come in "Multi-dimensional Arrays".

The same issue applies to Dicts, and we don't really talk about them in the
main manual at all.

--Tim

Mauro

unread,
Jul 9, 2014, 7:21:02 AM7/9/14
to julia...@googlegroups.com
You missed this: http://docs.julialang.org/en/latest/manual/variables-and-scoping/#constants

However, it is not very clear, in particular concerning constant type vs constant value.  So an update would be good.  Also, does it belong into the scope-section?

Steven G. Johnson

unread,
Jul 9, 2014, 11:07:35 AM7/9/14
to julia...@googlegroups.com


On Tuesday, July 8, 2014 11:34:10 PM UTC-4, Leah Hanson wrote:
I think I understand now, that `const` in Julia means that the type of the variable must remain constant. This was very surprising to me.

`const` in Julia is supposed to mean that the symbol is always bound to the same variable, I think.  Technically, you can assign it to another variable of the same type, but you get a warning as you saw above, and you probably shouldn't rely on this being allowed at all (e.g. Julia is free to inline the value of const variables when it compiles a function, so it may not notice if you subsequently change the const value.)  However, if the variable is a mutable type (e.g. Array, Dict, ...), then you can always change the contents of that variable.

So, for example,
       const foo = 3
is much like
       const int foo = 3;
in C: you are not supposed to change the value of foo from 3.

On the other hand,
       const foo = [4,7,1]
is much like
       int * const foo = (int*) malloc(sizeof(int) * 3);
       foo[0] = 4; foo[1] = 7; foo[2] = 1;
in C: foo is a constant pointer to non-constant data.   You can't change foo to point to a different array, but you can change the contents of the array that foo points to.
Reply all
Reply to author
Forward
0 new messages