{"id":2680,"date":"2023-12-29T12:00:43","date_gmt":"2023-12-29T11:00:43","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2680"},"modified":"2025-01-14T14:53:18","modified_gmt":"2025-01-14T13:53:18","slug":"6-4-scope-rules","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2680","title":{"rendered":"10.4 Scope rules"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2680&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2680&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Objects may have attributes in the form of data-items, methods and\/or classes. An attribute of an object has a name and the <em>scope<\/em> of such a name is the part of the program where the attribute may be <em>directly<\/em> accessed using the name. The name cannot be referred outside scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have seen several examples of attributes being directly accessed in the code. For the <code>withdraw<\/code> method in class <code>Account<\/code>, it accesses the parameter <code>amount<\/code> and data-item <code>balance<\/code> in the enclosing <code>Account<\/code>-object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the flight-example in section 6.2, we have seen that the method <code>delay<\/code> accesses the data-item <code>actualArrivalTime<\/code> in the enclosing <code>Flight<\/code>-class and <code>scheduledArrivalTime<\/code> in the <code>Route<\/code>-class enclosing the <code>Flight<\/code>-class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>direct scope<\/em> of a name of an attribute declaration is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the main-part of the object-descriptor (OD) containing the declaration<\/li>\n\n\n\n<li>all object-descriptors nested within OD<\/li>\n\n\n\n<li>all object-descriptors that directly or indirectly have OD as a super-descriptor.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following figure illustrates this definition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anObj: <strong>obj<\/strong>\n   x: <strong>var<\/strong> integer\n   <strong>class<\/strong> C:\n      y: <strong>var<\/strong> integer\n      M1:\n         z: <strong>var<\/strong> integer\n         -- x, y, and z may be accessed\n         -- anObj, C, D, and M1 may be accessed \n         -- a, b, and M2 cannot be accessed\n   <strong>class<\/strong> D:\n      a: <strong>var<\/strong> integer\n      M2:\n         b <strong>var<\/strong>: integer\n         -- x, x, and b may be accessed\n         -- anObj, C, D, and M2 may be accessed \n         -- y, z, and M1 cannot be accessed<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Double declaration.<\/em><\/strong> There are a number of additional rules to be considered. One of these is that <em>double declaration<\/em> of the same name is forbidden within an object-descriptor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anObj: <strong>obj<\/strong>\n   x: <strong>var<\/strong> integer\n   ...\n   x: <strong>ref<\/strong> Person<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above code fragment is illegal since it contains two declarations of <code>x<\/code>. This is independent of the kind of attribute being declared. The first <code>x<\/code> is the name of an integer variable, the second <code>x<\/code> is a reference to a <code>Person<\/code>-object, but still it is illegal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Redeclaration in a subclass<\/em><\/strong>. A subclass may not declare am attribute that has the same name as an attribute in a possible superclass:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Super:\n   ...\n   x: <strong>var<\/strong> integer\n   ...\n<strong>class<\/strong> Sub: Super\n   ...\n   x: <strong>ref<\/strong> T\n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The declaration <code>x: <strong>ref<\/strong> T<\/code> in class <code>Sub<\/code> is illegal since the superclass <code>Super<\/code> has an <code>x<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Name shadowing.<\/em><\/strong> An object-descriptor may declare an attribute, which has the same name as an attribute declared in an enclosing object-descriptor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anObj: <strong>obj<\/strong>\n   x: <strong>var<\/strong> integer\n   <strong>class<\/strong> C:\n      y: <strong>var<\/strong> integer\n      M1:\n         x: <strong>var<\/strong> integer\n         -- x here is the x declared in M1, not the one in anObj<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The declaration of <code>x<\/code> in <code>M1<\/code> shadows for the <code>x<\/code> declared in the enclosing object-descriptor of <code>anObj<\/code>. Name shadowing is often considered problematic in the sense that this can lead to confusion, as it may be unclear which name subsequent uses of the shadowed name refer to, especially in large programs. Name shadowing shall thus be used with care.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Local and global names.<\/em><\/strong> A name declared in an object-descriptor is a <em>local<\/em> <em>name<\/em> in the object-descriptor. Names declared in enclosing object-descriptors are called <em>global names<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Formal parameters.<\/strong><\/em> The name of a formal parameter of a class or method is considered a local name of the associated object-descriptor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Static\/lexical scoping.<\/em><\/strong> The kind of scoping described here is called <em>static<\/em> scoping or <em>lexical<\/em> scoping. This refers to the fact that the scope of a name is a property of the program text since it is possible to figure out the scope of a name by looking at the program text only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Dynamic scoping.<\/em><\/strong> There is also a form of scoping called <em>dynamic<\/em> scoping where the scoping of a name depends on the execution. It dates back to the first version of the Lisp-language, but few or no modern languages have dynamic scoping.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Remote scope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So far we have described the direct scope of a name, which is how a name may be accessed directly using just the name. If <code>x<\/code> is a name, then the direct scope of <code>x<\/code> is the object-descriptors where <code>x<\/code> may be accessed by just writing <code>x<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A name <code>x<\/code> may also be accessed by a <em>remote name<\/em> such as <code>r.x<\/code>. Any object-descriptor that potentially may have a reference to an object described by the object-descriptor where <code>x<\/code> is declared is in the <em>remote scope<\/em> of <code>x<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of this is attribute accessors like <code>account_1010.deposit(100)<\/code> and <code>account_1022.withdraw(200)<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is another example of using attribute accessors :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> T: \n   x: <strong>var<\/strong> integer\n   ...\nr: <strong>obj<\/strong> T\ns: <strong>ref<\/strong> T\ns := r         -- now s and r refer to the same object\nr.x := 117     -- assign 117 to the x attribute of r\ns.x := s.x + 1 -- increments the x attribute of s \n               -- which is the same as the x attribute of r<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>r.x<\/code> and <code>s.x<\/code> are both remote names and in this case they refer to the same <code>x<\/code> because <code>r<\/code> and <code>s<\/code> refer to the same <code>T<\/code> object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtual methods and classes and dynamic dispatch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A virtual method or class may be further specified in subclasses\/sub-descriptors. It is important to point out that such further specifications are not redeclaration\/redefinition of the virtual attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The actual virtual method invoked or actual virtual class instantiated by a virtual name is, however, first determined at run-time during program execution. Consider the example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Super:\n   V:&lt;\n     ...\n   <strong>class<\/strong> T:&lt;\n      ...\n<strong>class<\/strong> SubA: Super\n   V::&lt;\n      ...\n   <strong>class<\/strong> T::&lt; \n      ...\n<strong>class<\/strong> SubB: Super\n   V::&lt;\n      ...\n   <strong>class<\/strong> T::&lt; \n      ...\nR: <strong>ref<\/strong> Super  -- the static type of R is Super\n...\nR := SubA     -- the dynamic type of R is now SubA\n...\nR := SubB     -- the dynamic type of R is now SubB\n...\nR.V <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here we have a class <code>Super<\/code> with two virtual attributes <code>V<\/code> and <code>T.<\/code> We have two subclasses of <code>Super<\/code>, <code>SuperA<\/code> and <code>superB<\/code> which both contains bindings of <code>V<\/code> and <code>T<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have a reference variable <code>R<\/code> that may refer to instances of <code>Super<\/code> and to instances of subclasses of <code>Super<\/code>. As the example indicates <code>R<\/code> may at different places in the program refer to instances of <code>SubA<\/code> or instances of <code>SubB<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A remote invocation <code>R.V<\/code> instantiates the <code>V<\/code> being further specified in <code>SubA<\/code> or <code>SubB<\/code> depending on whether <code>R<\/code> refers to a <code>SubA<\/code>&#8211; or <code>SubB<\/code>-object. This is sometimes called <em>dynamic dispatch.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The type <code>Super<\/code> of <code>R<\/code> from the declaration of <code>R<\/code> is sometimes called the <em>static type<\/em> of <code>R<\/code> since it can be read from the program text. Since <code>R<\/code> may refer to instance of subtypes of <code>Super<\/code>, the type of the object being referred to at given point during the program execution is sometimes called the <em>dynamic type<\/em> of <code>R<\/code>. In the above example, the dynamic type may be either <code>SubA<\/code> or <code>SubB<\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>as indicated in the above code sketch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Restricting the scope of names<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometime it may be desirable to restrict the scope (direct as well as remote) of a name. In chapter <script>mkRef(\"Visibility mechanisms\")<\/script>, we introduce access modifiers as a mechanism for doing this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=2680\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Objects may have attributes in the form of data-items, methods and\/or classes. An attribute of an object has a name and the scope of such a name is the part of the program where the attribute may be directly accessed using the name. The name cannot be referred outside scope. We have seen several examples [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2680","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2680"}],"version-history":[{"count":52,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2680\/revisions"}],"predecessor-version":[{"id":11139,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2680\/revisions\/11139"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}