Debian Codegraph @

doc/book/src/scripting/scope.md

raw
1 # Scope
2
3 Scope generally works similar to other interpreted languages.
4
5 Modules have their own scope and cannot access any external variables (save
6 builtins such as meson, build\_machine, etc.).
7
8 Functions also get their own scope, but can see variables that have been
9 declared in parent scopes prior to the function definition. Function
10 definitions also capture their scope. Re-assigning to a variable after it has
11 been captured will not affect the value of the captured variable.
12
13 Example:
14
15 ```
16 func a() -> func
17 i = 0
18 func b() -> int
19 i += 1
20 return i
21 endfunc
22
23 return b
24 endfunc
25
26 counter = a()
27 counter() #=> 1
28 counter() #=> 2
29 new_counter = a()
30 new_counter() #=> 1
31 new_counter() #=> 2
32 counter() #=> 3
33 ```
34