Name Scopes
In this lesson, we discuss the name scopes.
We'll cover the following...
Name scope #
Any name is accessible from the point where it has been defined to the point where its scope ends, as well as in all of the scopes that its scope includes. In this regard, every scope defines a name scope.
Names are not available beyond the end of their scope:
Press + to interact
import std.stdio;void main() {bool aCondition = true;int outer;if (aCondition) { // ← curly bracket starts a new scopeint inner = 1;outer = 2; // ← 'outer' is available here} // ← 'inner' is not available beyond this pointinner = 3; // ← compilation ERROR// 'inner' is not available in the outer scope}
Because inner
is defined within the scope of the if
condition, it is available only in that scope. On the other hand, outer
is available in both the if block scope and main function scope.
It is not legal to define the same name in an inner scope:
size_t length = oddNumbers.length;
if (aCondition) {
size_t length = primeNumbers.length; // ←
...Access this course and 1400+ top-rated courses and projects.