From 2c856c20c19bb0c7e62185b9ae9109f721a19a0d Mon Sep 17 00:00:00 2001 From: Enzo Date: Wed, 11 Aug 2021 13:01:09 +0200 Subject: [PATCH] doc: document closures (#11139) --- doc/docs.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index 2d0a770a2c..7c664d64c2 100644 --- a/doc/docs.md +++ b/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