mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs: document dump(expr)
This commit is contained in:
parent
7ba13a415a
commit
a4fb851f3d
34
doc/docs.md
34
doc/docs.md
@ -124,6 +124,7 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
|
|||||||
* [v fmt](#v-fmt)
|
* [v fmt](#v-fmt)
|
||||||
* [Profiling](#profiling)
|
* [Profiling](#profiling)
|
||||||
* [Advanced Topics](#advanced-topics)
|
* [Advanced Topics](#advanced-topics)
|
||||||
|
* [Dumping expressions at runtime](#dumping-expressions-at-runtime)
|
||||||
* [Memory-unsafe code](#memory-unsafe-code)
|
* [Memory-unsafe code](#memory-unsafe-code)
|
||||||
* [Structs with reference fields](#structs-with-reference-fields)
|
* [Structs with reference fields](#structs-with-reference-fields)
|
||||||
* [sizeof and __offsetof](#sizeof-and-__offsetof)
|
* [sizeof and __offsetof](#sizeof-and-__offsetof)
|
||||||
@ -3284,6 +3285,39 @@ fn main() {
|
|||||||
|
|
||||||
# Advanced Topics
|
# Advanced Topics
|
||||||
|
|
||||||
|
## Dumping expressions at runtime
|
||||||
|
You can dump/trace the value of any V expression using `dump(expr)`.
|
||||||
|
For example, save this code sample as `factorial.v`, then run it with
|
||||||
|
`v run factorial.v`:
|
||||||
|
```v
|
||||||
|
fn factorial(n u32) u32 {
|
||||||
|
if dump(n <= 1) {
|
||||||
|
return dump(1)
|
||||||
|
}
|
||||||
|
return dump(n * factorial(n - 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println(factorial(5))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
You will get:
|
||||||
|
```
|
||||||
|
[factorial.v:2] n <= 1: false
|
||||||
|
[factorial.v:2] n <= 1: false
|
||||||
|
[factorial.v:2] n <= 1: false
|
||||||
|
[factorial.v:2] n <= 1: false
|
||||||
|
[factorial.v:2] n <= 1: true
|
||||||
|
[factorial.v:3] 1: 1
|
||||||
|
[factorial.v:5] n * factorial(n - 1): 2
|
||||||
|
[factorial.v:5] n * factorial(n - 1): 6
|
||||||
|
[factorial.v:5] n * factorial(n - 1): 24
|
||||||
|
[factorial.v:5] n * factorial(n - 1): 120
|
||||||
|
120
|
||||||
|
```
|
||||||
|
Note that `dump(expr)` will trace both the source location,
|
||||||
|
the expression itself, and the expression value.
|
||||||
|
|
||||||
## Memory-unsafe code
|
## Memory-unsafe code
|
||||||
|
|
||||||
Sometimes for efficiency you may want to write low-level code that can potentially
|
Sometimes for efficiency you may want to write low-level code that can potentially
|
||||||
|
Loading…
Reference in New Issue
Block a user