From 6cffcf515a2a0f33d97d92a7b1321dedb95d7de3 Mon Sep 17 00:00:00 2001 From: cindRoberta <32280512+cindRoberta@users.noreply.github.com> Date: Thu, 30 Sep 2021 06:43:26 -0300 Subject: [PATCH] docs: add the topics Hoistings and Closures (#12021) --- doc/docs.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index 88bad0e761..e1353851cb 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -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.