Scope
Global scope
The global scope is a declarative space where the declarations' order is not important. This is the only scope in which you can declare enums, objects and protocols.
object B {
| Here `A` is referred to before its declaration
A a,
}
object A {
str value,
}
| Only a constant value is allowed here
str globalValue = "hello";
Local scope
Any variable declared inside a block is local to it. Shadowing a variable from an upper scope is not allowed.
str bye = "bye";
while (condition) {
str hello = "hello"; | Local to this block
int bye = 12; | -> Not allowed
}
| hello doesn't exist anymore
Upvalues
If a reference to a variable from a upper scope is made inside a function's body, the function will carry a reference to it even when the variable goes out of scope. This is an upvalue.
fun getFn() > Function () > void {
str upvalue = "up there";
Function () > void hello = fun () -> print(upvalue);
return hello;
}
| `upvalue` is out of scope but still exists as an upvalue of `hello`
getFn()();