doc: improve docs for goto, sizeof, __offsetof (#8522)
parent
7875164d91
commit
9f662002da
49
doc/docs.md
49
doc/docs.md
|
@ -63,6 +63,7 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
|
||||||
* [Strings](#strings)
|
* [Strings](#strings)
|
||||||
* [Numbers](#numbers)
|
* [Numbers](#numbers)
|
||||||
* [Arrays](#arrays)
|
* [Arrays](#arrays)
|
||||||
|
* [Fixed size arrays](#fixed-size-arrays)
|
||||||
* [Maps](#maps)
|
* [Maps](#maps)
|
||||||
* [Module imports](#module-imports)
|
* [Module imports](#module-imports)
|
||||||
* [Statements & expressions](#statements--expressions)
|
* [Statements & expressions](#statements--expressions)
|
||||||
|
@ -754,31 +755,32 @@ array_2 << array_1[..3]
|
||||||
println(array_2) // [0, 1, 3, 5, 4]
|
println(array_2) // [0, 1, 3, 5, 4]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Fixed Size Arrays
|
### Fixed size arrays
|
||||||
V also supports arrays with fixed size. Unlike ordinary arrays, their
|
|
||||||
length is fixed, so you can not append elements to them, nor shrink them.
|
|
||||||
You can only modify their elements in place. Note also, that most methods
|
|
||||||
are defined to work on ordinary arrays, not on fixed size arrays.
|
|
||||||
|
|
||||||
However, access to the elements of fixed size arrays, is more efficient,
|
V also supports arrays with fixed size. Unlike ordinary arrays, their
|
||||||
|
length is constant. You cannot append elements to them, nor shrink them.
|
||||||
|
You can only modify their elements in place.
|
||||||
|
|
||||||
|
However, access to the elements of fixed size arrays is more efficient,
|
||||||
they need less memory than ordinary arrays, and unlike ordinary arrays,
|
they need less memory than ordinary arrays, and unlike ordinary arrays,
|
||||||
their data is on the stack, so you may want to use them as buffers if you
|
their data is on the stack, so you may want to use them as buffers if you
|
||||||
do not want additional heap allocations.
|
do not want additional heap allocations.
|
||||||
|
|
||||||
You can convert a fixed size array, to an ordinary array with slicing:
|
Most methods are defined to work on ordinary arrays, not on fixed size arrays.
|
||||||
|
You can convert a fixed size array to an ordinary array with slicing:
|
||||||
```v
|
```v
|
||||||
mut fnums := [3]int{} // fnums is now a fixed size array with 3 elements.
|
mut fnums := [3]int{} // fnums is a fixed size array with 3 elements.
|
||||||
fnums[0] = 1
|
fnums[0] = 1
|
||||||
fnums[1] = 10
|
fnums[1] = 10
|
||||||
fnums[2] = 100
|
fnums[2] = 100
|
||||||
println(fnums) // => [1, 10, 100]
|
println(fnums) // => [1, 10, 100]
|
||||||
println(typeof(fnums).name) // => [3]int
|
println(typeof(fnums).name) // => [3]int
|
||||||
//
|
|
||||||
anums := fnums[0..fnums.len]
|
anums := fnums[0..fnums.len]
|
||||||
println(anums) // => [1, 10, 100]
|
println(anums) // => [1, 10, 100]
|
||||||
println(typeof(anums).name) // => []int
|
println(typeof(anums).name) // => []int
|
||||||
```
|
```
|
||||||
Note that slicing will cause the data of the fixed array, to be copied to
|
Note that slicing will cause the data of the fixed size array to be copied to
|
||||||
the newly created ordinary array.
|
the newly created ordinary array.
|
||||||
|
|
||||||
### Maps
|
### Maps
|
||||||
|
@ -3065,8 +3067,8 @@ println(qux)
|
||||||
|
|
||||||
## sizeof and __offsetof
|
## sizeof and __offsetof
|
||||||
|
|
||||||
V supports the usage of `sizeof` to calculate sizes of structs and
|
* `sizeof(Type)` gives the size of a type in bytes.
|
||||||
`__offsetof` to calculate struct field offsets.
|
* `__offsetof(Struct, field_name)` gives the offset in bytes of a struct field.
|
||||||
|
|
||||||
```v
|
```v
|
||||||
struct Foo {
|
struct Foo {
|
||||||
|
@ -3074,9 +3076,9 @@ struct Foo {
|
||||||
b int
|
b int
|
||||||
}
|
}
|
||||||
|
|
||||||
println(sizeof(Foo))
|
assert sizeof(Foo) == 8
|
||||||
println(__offsetof(Foo, a))
|
assert __offsetof(Foo, a) == 0
|
||||||
println(__offsetof(Foo, b))
|
assert __offsetof(Foo, b) == 4
|
||||||
```
|
```
|
||||||
|
|
||||||
## Calling C functions from V
|
## Calling C functions from V
|
||||||
|
@ -3850,14 +3852,23 @@ fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int)
|
||||||
|
|
||||||
## Goto
|
## Goto
|
||||||
|
|
||||||
V allows unconditionally jumping to arbitrary labels with `goto`. Labels must be contained
|
V allows unconditionally jumping to a label with `goto`. The label name must be contained
|
||||||
within the text document from where they are jumped to. A program may `goto` a label outside
|
within the same function as the `goto` statement. A program may `goto` a label outside
|
||||||
or deeper than the current scope, but it cannot `goto` a label inside of a different function.
|
or deeper than the current scope, but it must not skip a variable initialization.
|
||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
|
if x {
|
||||||
|
// ...
|
||||||
|
if y {
|
||||||
|
goto my_label
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
}
|
||||||
my_label:
|
my_label:
|
||||||
goto my_label
|
|
||||||
```
|
```
|
||||||
|
`goto` should be avoided when `for` can be used instead. In particular,
|
||||||
|
[labelled break](#labelled-break--continue) can be used to break out of
|
||||||
|
a nested loop.
|
||||||
|
|
||||||
# Appendices
|
# Appendices
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue