mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: document the new const rule
This commit is contained in:
parent
80e6b090ea
commit
12ff1c2702
15
doc/docs.md
15
doc/docs.md
@ -1716,15 +1716,22 @@ const (
|
||||
blue = rgb(0, 0, 255)
|
||||
)
|
||||
|
||||
println(numbers)
|
||||
println(red)
|
||||
println(blue)
|
||||
println(main.numbers)
|
||||
println(main.red)
|
||||
println(main.blue)
|
||||
```
|
||||
|
||||
Global variables are not allowed, so this can be really useful.
|
||||
|
||||
When naming constants, `snake_case` must be used. In order to distinguish consts
|
||||
from local variables, the full path to consts must be specified. For example,
|
||||
to access the PI const, full `math.pi` name must be used both outside the `math` module,
|
||||
and inside it. This can be seen in the example above with `println(main.numbers)`.
|
||||
|
||||
vfmt takes care of this rule, so you can type `println(pi)` inside the `math` module,
|
||||
and vffmt will automatically update it to `println(math.pi)`.
|
||||
|
||||
<!--
|
||||
When naming constants, snake_case must be used.
|
||||
Many people prefer all caps consts: `TOP_CITIES`. This wouldn't work
|
||||
well in V, because consts are a lot more powerful than in other languages.
|
||||
They can represent complex structures, and this is used quite often since there
|
||||
|
@ -1,7 +0,0 @@
|
||||
vlib/v/checker/tests/var_duplicate_const.vv:8:2: error: duplicate of a const name `age`
|
||||
6 |
|
||||
7 | fn main() {
|
||||
8 | age := 30
|
||||
| ~~~
|
||||
9 | println(age)
|
||||
10 | }
|
Loading…
Reference in New Issue
Block a user