Block Scopes and the Local Modifier - Spicery/Nutmeg GitHub Wiki
Block scopes
The block ... endblock
is used to create a scope with fine-grained control over what is visible and what is not. Unlike a normal lexical scope, where new variables are invisible outside the scope, variables introduced within a block-scope are visible by default. If you want to 'hide' a variable declaration in a block you use the local
modifier. The declaration to which this is applied must be at block-scope level, of course.
In the following example, we have two scoped declarations for factorial
and f
respectively but only factorial
is visible from the outside. The use of @nonlocal is optional but helps make the factorial
definition stand out.
block
@local
def f( n, sofar ):
if n <= 1:
sofar
else:
n( n - 1, sofar * n )
endif
end
def factorial( n ): f( n, 1 ) end
endblock
Block scopes can always be introduced implicitly by other syntax. For example, the class ... endclass
introduces a block-scope.