Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Solidity Scoping – A Helpful Guide with Video

#1
Solidity Scoping – A Helpful Guide with Video

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1191575&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>As promised in the <a rel="noreferrer noopener" href="https://blog.finxter.com/solidity-declarations-a-helpful-guide-with-video/" data-type="post" data-id="1191512" target="_blank">previous article</a>, we’ll get more closely familiar with the concept of scoping next. We’ll explain what scoping is, why it exists, and how it helps us in programming.</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/solidity-scoping/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJmEVpzi6OFQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="572" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-98-1024x572.png" alt="" class="wp-image-1191636" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-98-1024x572.png 1024w, https://blog.finxter.com/wp-content/uplo...00x168.png 300w, https://blog.finxter.com/wp-content/uplo...68x429.png 768w, https://blog.finxter.com/wp-content/uplo...age-98.png 1060w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>It’s part of our long-standing tradition to make this (and other) articles a faithful companion, or a supplement to the <a rel="noreferrer noopener" href="https://docs.soliditylang.org/en/v0.8.17/control-structures.html#scoping-and-declarations" data-type="URL" data-id="https://docs.soliditylang.org/en/v0.8.17/control-structures.html#scoping-and-declarations" target="_blank">official Solidity documentation</a>.</p>
<h2>Scopes Overview</h2>
<p>Scope refers to the context in which we can access a defined variable or a function. There are three main types of scope specific to Solidity: </p>
<ul>
<li><code>global</code>, </li>
<li><code>contract</code>, and </li>
<li><code>function</code> scope.</li>
</ul>
<p>In the <strong>global scope</strong>, variables, and functions are defined at the global level, i.e., outside of any contract or function, and we can access them from any place in the source code.</p>
<p>In the <strong>contract scope</strong>, variables and functions are defined within a contract, but outside of any function, so we can access them from anywhere within the specific contract. However, these variables and functions are inaccessible from outside the contract scope.</p>
<p>In the <strong>function scope</strong>, variables and functions are defined within a function and we can access them exclusively from inside that function.</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="570" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-99-1024x570.png" alt="" class="wp-image-1191639" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-99-1024x570.png 1024w, https://blog.finxter.com/wp-content/uplo...00x167.png 300w, https://blog.finxter.com/wp-content/uplo...68x428.png 768w, https://blog.finxter.com/wp-content/uplo...age-99.png 1068w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: </p>
<p>The concept of scopes in Solidity is similar and based on the concept of scopes in the C99 programming language. In both languages, a “scope” refers to the context in which a variable or function is defined and can be accessed.</p>
<p>In C99 (a C language standard from 1999), variables and functions can be defined at either the global level (i.e., outside of any function) or within a function. There is no “contract” scope in C99.</p>
<h2>Global Scope</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="574" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-100-1024x574.png" alt="" class="wp-image-1191642" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-100-1024x574.png 1024w, https://blog.finxter.com/wp-content/uplo...00x168.png 300w, https://blog.finxter.com/wp-content/uplo...68x430.png 768w, https://blog.finxter.com/wp-content/uplo...ge-100.png 1064w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Let’s take a look at a simple example of the global scope:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pragma solidity ^0.6.12; uint public globalCounter; function incrementGlobalCounter() public { globalCounter++;
}
</pre>
<p>In this example, the <code>globalCounter</code> variable is defined at the global level and is, therefore, in the global scope. We can access it from anywhere in the code, including from within the <code>incrementGlobalCounter(...)</code> function.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Reminder</strong>: Global variables and functions can be accessed and modified by any contract or function that has access to them. We can find this behavior useful for sharing data across contracts or functions, but it can also present security risks if the global variables or functions are not properly protected.</p>
<h2>Contract Scope</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="571" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-101-1024x571.png" alt="" class="wp-image-1191643" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-101-1024x571.png 1024w, https://blog.finxter.com/wp-content/uplo...00x167.png 300w, https://blog.finxter.com/wp-content/uplo...68x428.png 768w, https://blog.finxter.com/wp-content/uplo...ge-101.png 1066w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>As explained above, variables and functions defined within a contract (but outside of any function) are in contract scope, and we can access them from anywhere within the contract. </p>
<p>Contract-level variables and functions are useful for storing and manipulating data that is specific to a particular contract and is not meant to be shared with other contracts or functions.</p>
<p>Let’s take a look at a simple example of the contract scope:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pragma solidity ^0.6.12; contract Counter { uint public contractCounter; function incrementContractCounter() public { contractCounter++; }
}
</pre>
<p>In this example, the <code>contractCounter</code> variable is defined within the <code>Counter</code> contract and is, therefore, in contract scope. It is available for access from anywhere within the <code>Counter</code> contract, including from within the <code>incrementContractCounter()</code> function.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Warning</strong>: We should be aware that contract-level variables and functions are only accessible from within the contract in which they are defined. They cannot be accessed from other contracts or from external accounts.</p>
<h2>Function Scope</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="572" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-102-1024x572.png" alt="" class="wp-image-1191645" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-102-1024x572.png 1024w, https://blog.finxter.com/wp-content/uplo...00x167.png 300w, https://blog.finxter.com/wp-content/uplo...68x429.png 768w, https://blog.finxter.com/wp-content/uplo...ge-102.png 1064w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Variables and functions that are defined within a function are in the function scope and can only be accessed from within that function. </p>
<p>Function-level variables and functions are useful for storing and manipulating data that is specific to a particular function and is not meant to be shared with other functions or with the contract as a whole.</p>
<p>Let’s take a look at the following example of the function scope:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pragma solidity ^0.6.12; contract Counter { function incrementCounter(uint incrementAmount) public { uint functionCounter = 0; functionCounter += incrementAmount; }
}
</pre>
<p>In this example, the <code>functionCounter</code> variable is defined within the <code>incrementCounter(...)</code> function and is, therefore, in the function scope. It can only be accessed from within the <code>incrementCounter</code> function and is not accessible from other functions or from outside the contract.</p>
<h2>C99 Scoping Rules</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="574" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-103-1024x574.png" alt="" class="wp-image-1191646" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-103-1024x574.png 1024w, https://blog.finxter.com/wp-content/uplo...00x168.png 300w, https://blog.finxter.com/wp-content/uplo...68x430.png 768w, https://blog.finxter.com/wp-content/uplo...ge-103.png 1064w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Now, let’s take a look at an interesting example showing minimal scoping by using curly braces:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 &lt;0.9.0;
contract C { function minimalScoping() pure public { { uint same; same = 1; } { uint same; same = 3; } }
}
</pre>
<p>Each of the curly braces pair forms a distinct scope, containing a declaration and initialization of the variable same. </p>
<p>This example will compile without warnings or errors because each of the variable’s lifecycles is contained in its own disjoint scope, and there is no overlap between the two scopes.</p>
<h2>Shadowing</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="572" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-104-1024x572.png" alt="" class="wp-image-1191648" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-104-1024x572.png 1024w, https://blog.finxter.com/wp-content/uplo...00x167.png 300w, https://blog.finxter.com/wp-content/uplo...68x429.png 768w, https://blog.finxter.com/wp-content/uplo...ge-104.png 1066w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>In some special cases, such as this one demonstrating C99 scoping rules below, we’d come across a phenomenon called shadowing. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Shadowing</strong> means that two or more variables share their name and have intersected scopes, with the first one as the outer scope and the second one as the inner scope. </p>
<p>Let’s take a closer look to get a better idea of what’s all about:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 &lt;0.9.0;
// This will report a warning
contract C { function f() pure public returns (uint) { uint x = 1; { x = 2; // this will assign to the outer variable uint x; } return x; // x has value 2 }
}
</pre>
<p>There are two variables called <code>x</code>; the first one is in the outer scope, and the second one is in the inner scope. </p>
<p>The inner scope is contained in or surrounded by the outer scope. </p>
<p>Therefore, the first and the second assignment assign the value 1, and then value 2 to the outer variable <code>x</code>, and only then will the declaration of the second variable <code>x</code> take place. </p>
<p>In this specific case, we’d get a warning from the <a href="https://blog.finxter.com/how-to-install-the-solidity-compiler-overview-videos/" data-type="post" data-id="716526" target="_blank" rel="noreferrer noopener">compiler</a>, because the first (outer) variable <code>x</code> is being shadowed by the second variable <code>x</code>.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Warning</strong>: in versions prior to 0.5.0, Solidity used the same scoping rules as JavaScript: a variable declared at any location within the function would be visible through the entire function’s scope. That’s why the example below could’ve been compiled in Solidity versions before 0.5.0:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 &lt;0.9.0;
// This will not compile
contract C { function f() pure public returns (uint) { x = 2; uint x; return x; }
}
</pre>
<p>The code above couldn’t compile in today’s versions of Solidity because an assignment to variable <code>x</code> is attempted before the variable itself is declared. In other words, the inner variable <code>x</code>‘s scope starts with the line of its declaration.</p>
<h2><a></a>Conclusion</h2>
<p>In this article, we learned about variable and function scopes.</p>
<ul>
<li>First, we made a scope overview, introducing ourselves to three different scopes in Solidity.</li>
<li>Second, we investigated the global scope by studying an appropriate example.</li>
<li>Third, we looked at the contract scope through an appropriate example.</li>
<li>Third, learned about the function scope on an appropriate example.</li>
<li>Fourth, we glanced at C99 scoping rules based on C99 – a C language standard.</li>
<li>Fifth, we also learned about shadowing and got an idea of why we should be careful about it.</li>
</ul>
<h2>What’s Next?</h2>
<p>This tutorial is part of our extended Solidity documentation with videos and more accessible examples and explanations. You can navigate the series here (all links open in a new tab):</p>
<div class="is-content-justification-center is-layout-flex wp-container-1 wp-block-buttons">
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://blog.finxter.com/solidity-declarations-a-helpful-guide-with-video/" target="_blank" rel="noreferrer noopener"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f448.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Prev Tutorial</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://blog.finxter.com/solidity-deep-dive-syllabus-video-tutorial-resources/" target="_blank" rel="noreferrer noopener"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/261d.png" alt="☝" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Syllabus</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://blog.finxter.com/solidity-declarations-a-helpful-guide-with-video/" target="_blank" rel="noreferrer noopener"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Next Tutorial</a></div>
</div>
</div>


https://www.sickgaming.net/blog/2023/03/...ith-video/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016