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.