mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
docs: add a section for static variables (#23184)
This commit is contained in:
parent
03f3f7a9ff
commit
1fae506900
1 changed files with 36 additions and 0 deletions
36
doc/docs.md
36
doc/docs.md
|
@ -195,6 +195,7 @@ by using any of the following commands in a terminal:
|
||||||
* [Performance tuning](#performance-tuning)
|
* [Performance tuning](#performance-tuning)
|
||||||
* [Atomics](#atomics)
|
* [Atomics](#atomics)
|
||||||
* [Global Variables](#global-variables)
|
* [Global Variables](#global-variables)
|
||||||
|
* [Static Variables](#static-variables)
|
||||||
* [Cross compilation](#cross-compilation)
|
* [Cross compilation](#cross-compilation)
|
||||||
* [Debugging](#debugging)
|
* [Debugging](#debugging)
|
||||||
* [C Backend binaries Default](#c-backend-binaries-default)
|
* [C Backend binaries Default](#c-backend-binaries-default)
|
||||||
|
@ -7328,6 +7329,41 @@ to race conditions. There are several approaches to deal with these:
|
||||||
correlated, which is acceptable considering the performance penalty that using
|
correlated, which is acceptable considering the performance penalty that using
|
||||||
synchronization primitives would represent.
|
synchronization primitives would represent.
|
||||||
|
|
||||||
|
## Static Variables
|
||||||
|
|
||||||
|
V also supports *static variables*, which are like *global variables*, but
|
||||||
|
available only *inside* a single unsafe function (you can look at them as
|
||||||
|
namespaced globals).
|
||||||
|
|
||||||
|
Note: their use is discouraged too, for reasons similar to why globals
|
||||||
|
are discouraged. The feature is supported to enable translating existing
|
||||||
|
low level C code into V code, using `v translate`.
|
||||||
|
|
||||||
|
Note: the function in which you use a static variable, has to be marked
|
||||||
|
with @[unsafe]. Also unlike using globals, using static variables, do not
|
||||||
|
require you to pass the flag `-enable-globals`, because they can only be
|
||||||
|
read/changed inside a single function, which has full control over the
|
||||||
|
state stored in them.
|
||||||
|
|
||||||
|
Here is a small example of how static variables can be used:
|
||||||
|
```v
|
||||||
|
@[unsafe]
|
||||||
|
fn counter() int {
|
||||||
|
mut static x := 42
|
||||||
|
// Note: x is initialised to 42, just _once_.
|
||||||
|
x++
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f() int {
|
||||||
|
return unsafe { counter() }
|
||||||
|
}
|
||||||
|
|
||||||
|
println(f()) // prints 43
|
||||||
|
println(f()) // prints 44
|
||||||
|
println(f()) // prints 45
|
||||||
|
```
|
||||||
|
|
||||||
## Cross compilation
|
## Cross compilation
|
||||||
|
|
||||||
Cross compilation is supported for Windows, Linux and FreeBSD.
|
Cross compilation is supported for Windows, Linux and FreeBSD.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue