docs: add the topics Hoistings and Closures (#12021)

pull/12024/head
cindRoberta 2021-09-30 06:43:26 -03:00 committed by GitHub
parent e3d379a1eb
commit 6cffcf515a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -65,6 +65,7 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
* [Comments](#comments)
* [Functions](#functions)
* [Returning multiple values](#returning-multiple-values)
* [Hoistings](#hoistings)
* [Symbol visibility](#symbol-visibility)
* [Variables](#variables)
* [V types](#v-types)
@ -95,6 +96,7 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
* [Mutable arguments](#mutable-arguments)
* [Variable number of arguments](#variable-number-of-arguments)
* [Anonymous & higher-order functions](#anonymous--higher-order-functions)
* [Closures](#closures)
* [References](#references)
* [Constants](#constants)
* [Builtin functions](#builtin-functions)
@ -264,6 +266,8 @@ Again, the type comes after the argument's name.
Just like in Go and C, functions cannot be overloaded.
This simplifies the code and improves maintainability and readability.
### Hoistings
Functions can be used before their declaration:
`add` and `sub` are declared after `main`, but can still be called from `main`.
This is true for all declarations in V and eliminates the need for header files
@ -2137,6 +2141,8 @@ fn main() {
}
```
### Closures
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.