Skip to content

Commit

Permalink
stronger warnings about changing constants in help and docs (#28711)
Browse files Browse the repository at this point in the history
[ci skip]

(cherry picked from commit 4b103ef)
  • Loading branch information
JeffBezanson authored and KristofferC committed Feb 11, 2019
1 parent 87a72e5 commit 0adbe7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 10 additions & 8 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ kw"'"
"""
const
`const` is used to declare global variables which are also constant. In almost all code
`const` is used to declare global variables whose values will not change. In almost all code
(and particularly performance sensitive code) global variables should be declared
constant in this way.
Expand All @@ -275,15 +275,17 @@ const y, z = 7, 11
Note that `const` only applies to one `=` operation, therefore `const x = y = 1`
declares `x` to be constant but not `y`. On the other hand, `const x = const y = 1`
declares both `x` and `y` as constants.
declares both `x` and `y` constant.
Note that "constant-ness" is not enforced inside containers, so if `x` is an array or
dictionary (for example) you can still add and remove elements.
Note that "constant-ness" does not extend into mutable containers; only the
association between a variable and its value is constant.
If `x` is an array or dictionary (for example) you can still modify, add, or remove elements.
Technically, you can even redefine `const` variables, although this will generate a
warning from the compiler. The only strict requirement is that the *type* of the
variable does not change, which is why `const` variables are much faster than regular
globals.
In some cases changing the value of a `const` variable gives a warning instead of
an error.
However, this can produce unpredictable behavior or corrupt the state of your program,
and so should be avoided.
This feature is intended only for convenience during interactive use.
"""
kw"const"

Expand Down
7 changes: 5 additions & 2 deletions doc/src/manual/variables-and-scoping.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,11 @@ WARNING: redefining constant a
1
```

Note that although possible, changing the value of a variable that is declared as constant
is strongly discouraged. For instance, if a method references a constant and is already
Note that although sometimes possible, changing the value of a `const` variable
is strongly discouraged, and is intended only for convenience during
interactive use.
Changing constants can cause various problems or unexpected behaviors.
For instance, if a method references a constant and is already
compiled before the constant is changed then it might keep using the old value:
```jldoctest
julia> const x = 1
Expand Down

0 comments on commit 0adbe7e

Please sign in to comment.