lotus



previous page: L17) What is the rationale behind the syntax of BETA?
  
page up: BETA Programming Language FAQ
  
next page: L19) What is a pattern?

L18) How do the scope rules of BETA actually work?




Description

This article is from the FAQ, by with numerous contributions by others.

L18) How do the scope rules of BETA actually work?

The BETA scope rules may seem slightly complex to the new BETA programmer,
but are actually rather intuitive and simple. There are three visibility
rules:

1. Names declared in the descriptor itself are visible within the
descriptor.
2. Names declared in the superpattern of a descriptor are visible within
the descriptor.
3. Names declared in outer blocks (i.e. enclosing descriptors) are visible
within the descriptor.

These rules are applied in order to find the definition for a given name
application:

* Start by using rule 1, looking for a local declaration.
* If not found, then use rule 2 to find the declaration in the
superpattern (if the descriptor has one). While using this rule, you
may apply rule 1.
* If still not found, then use rule 3 to find the declaration in the
enclosing descriptors. While using this rule, you may apply rule 1 and
2.

Note: This method implies that it is possible to reuse the same name for
different declarations, as long as the declarations are in different
descriptors.

To see how the rules interact, take a look at the example program below. It
illustrates most of the combinations, and has the resulting output is shown
in comments after each imperative.

   (# a: (# do 1->screen.putint #);
      P1: (# do a; INNER P1 #);
      P2: (# a: (# do 2->screen.putint #);
          do a
          #);
      P3: P1(# do a #);
      P4: P1(# a: (# do 3->screen.putint #);
          do a
          #);
      P5: P1(# foo1: (# do a; inner foo1 #);
               foo2: (# a: (# do 4->screen.putint #)
                     do a; inner foo2
                     #);
            #);
      P6: P5(# a: (# do 5->screen.putint #);
               foo3: (# do a; inner foo3 #);
               foo4: foo1(# do a; inner foo4 #);
               foo5: foo2(# do a; inner foo5 #);
            #);
      P: @P6;
   do
      a;                (*   1 *)
      P1;               (*   1 *)
      P2;               (*   2 *)
      P3;               (*  11 *)
      P4;               (*  13 *)
      P5;               (*   1 *)
      P6;               (*   1 *)

      P.foo1;           (*   1 *)
      P.foo2;           (*   4 *)
      P.foo3;           (*   5 *)
      P.foo4;           (*  15 *)
      P.foo5;           (*  44 *)

      P.foo1(# do a #); (*  11 *)
      P.foo2(# do a #); (*  44 *)
      P.foo3(# do a #); (*  51 *)
      P.foo4(# do a #); (* 151 *)
      P.foo5(# do a #); (* 444 *)
   #)


 

Continue to:















TOP
previous page: L17) What is the rationale behind the syntax of BETA?
  
page up: BETA Programming Language FAQ
  
next page: L19) What is a pattern?