Skip to content

Commit 4b103ef

Browse files
authored
stronger warnings about changing constants in help and docs (#28711)
[ci skip]
1 parent 5c1d4a3 commit 4b103ef

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

base/docs/basedocs.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ kw"'"
260260
"""
261261
const
262262
263-
`const` is used to declare global variables which are also constant. In almost all code
263+
`const` is used to declare global variables whose values will not change. In almost all code
264264
(and particularly performance sensitive code) global variables should be declared
265265
constant in this way.
266266
@@ -275,15 +275,17 @@ const y, z = 7, 11
275275
276276
Note that `const` only applies to one `=` operation, therefore `const x = y = 1`
277277
declares `x` to be constant but not `y`. On the other hand, `const x = const y = 1`
278-
declares both `x` and `y` as constants.
278+
declares both `x` and `y` constant.
279279
280-
Note that "constant-ness" is not enforced inside containers, so if `x` is an array or
281-
dictionary (for example) you can still add and remove elements.
280+
Note that "constant-ness" does not extend into mutable containers; only the
281+
association between a variable and its value is constant.
282+
If `x` is an array or dictionary (for example) you can still modify, add, or remove elements.
282283
283-
Technically, you can even redefine `const` variables, although this will generate a
284-
warning from the compiler. The only strict requirement is that the *type* of the
285-
variable does not change, which is why `const` variables are much faster than regular
286-
globals.
284+
In some cases changing the value of a `const` variable gives a warning instead of
285+
an error.
286+
However, this can produce unpredictable behavior or corrupt the state of your program,
287+
and so should be avoided.
288+
This feature is intended only for convenience during interactive use.
287289
"""
288290
kw"const"
289291

doc/src/manual/variables-and-scoping.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,11 @@ WARNING: redefining constant a
555555
1
556556
```
557557

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

0 commit comments

Comments
 (0)