Lexical ScopeΒΆ
A lexical scope is a region of the source code in which a set of referable identifiers are made available.
Note
First, all members of a structural value are made available to each other. For instance, in the following snippet:
{
x = y
y = 'value'
}
the value of x
is the 'value'
string because y
is defined in the same structural value as x
, and is therefore in the same scope as x
.
The members of a structural value are also made available to its nested structural values. In other words, identifiers that are at a lower level can be accessed in structural values that are at a higher level. This allows the following value to be completely legal:
{
v = 'value'
x = {
a = v
}
}
Next, all type members of a type are also made available to each other. This is generally used to specify a type relationship between generic members, and to ascribe contract members with the identifier of a locally defined generic member. For instance, the following snippet:
[
T <: Any
some: T
else: T
]
represents a type that accepts only values which contain some
and else
concrete members with a common subtype T
.
Tip
Moving nested structural values to a lower level in the source code is likely to reduce the amount of identifiers that exist simultaneously in the context, therefore reducing code complexity.