doc: document closures (#11139)
parent
8c2f7901aa
commit
2c856c20c1
63
doc/docs.md
63
doc/docs.md
|
@ -2110,6 +2110,69 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
V supports closures too.
|
||||
This means that anonymous functions can inherit variables from the scope they were created in.
|
||||
They must do so explicitly by listing all variables that are inherited.
|
||||
|
||||
> Warning: currently works on Unix-based, x64 architectures only.
|
||||
Some work is in progress to make closures work on Windows, then other architectures.
|
||||
|
||||
```v
|
||||
my_int := 1
|
||||
my_closure := fn [my_int] () {
|
||||
println(my_int)
|
||||
}
|
||||
my_closure() // prints 1
|
||||
```
|
||||
|
||||
Inherited variables are copied when the anonymous function is created.
|
||||
This means that if the original variable is modified after the creation of the function,
|
||||
the modification won't be reflected in the function.
|
||||
|
||||
```v
|
||||
mut i := 1
|
||||
func := fn [i] () int {
|
||||
return i
|
||||
}
|
||||
println(func() == 1) // true
|
||||
i = 123
|
||||
println(func() == 1) // still true
|
||||
```
|
||||
|
||||
However, the variable can be modified inside the anonymous function.
|
||||
The change won't be reflected outside, but will be in the later function calls.
|
||||
|
||||
```v
|
||||
fn new_counter() fn () int {
|
||||
mut i := 0
|
||||
return fn [mut i] () int {
|
||||
i++
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
c := new_counter()
|
||||
println(c()) // 1
|
||||
println(c()) // 2
|
||||
println(c()) // 3
|
||||
```
|
||||
|
||||
If you need the value to be modified outside the function, use a reference.
|
||||
**Warning**: _you need to make sure the reference is always valid,
|
||||
otherwise this can result in undefined behavior._
|
||||
|
||||
```v
|
||||
mut i := 0
|
||||
mut ref := &i
|
||||
print_counter := fn [ref] () {
|
||||
println(*ref)
|
||||
}
|
||||
|
||||
print_counter() // 0
|
||||
i = 10
|
||||
print_counter() // 10
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
```v
|
||||
|
|
Loading…
Reference in New Issue