Re: rant // OE scope \\ forked out of 500 Rosetta

new topic     » goto parent     » topic index » view thread      » older message » newer message

More Space

It is natural to write large programs made up of small files. When you include a file from the standard library this is what you are doing.

A large program made up of small files:

Here are two files (two spaces) that will be used to make one program. Both are inside the Euphoria Interpreter space so the each can use all of the built-in routines.

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐Interpreter 
│      ┌╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │           │     │           │    │ 
│      └╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
		two independent files 
 

By default, what is written in one file is invisible all other files. Think of each file space as being water-tight and no identifiers "leak" out of that file.

To build a larger program out of smaller files you must include each file.

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │include myinc.e │     │atom x     │    │ 
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 
	  x is out-of-scope 
 
 

The identifiers in each file remain invisible to each other (still water-tight). You need an extra step before included identifiers become visible; you must let identifiers leave a file space with a special keyword like global. You must create a "leak" so identifiers can flow out of a file space.

Included Variable

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │include myinc.e │     │global atom x │    │ 
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 
 
 

The scope for "x" now looks like:

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │                │     │              │    │ 
│      │include myinc.e │     │global atom x │    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░│    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░│    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░│    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░│    │ 
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 
	   x is in-scope 
 

For a variable, the include statement acts like the declaration point for the included identifier "x". The scope for both files still follows the "waterfall" idea of scope goes from the declaration to the end of space.

Included Routine

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │include myinc.e │     │procedure foo() │    │ 
│      │                │     │                │    │  
│      │                │     │end procedure   │    │  
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 

The identifiers is each file remain invisible to each other; the file spaces are "water-tight".

To make an identifier visible to another file you need a special keyword like global.

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │include myinc.e │     │global procedure foo() │    │ 
│      │                │     │                       │    │  
│      │                │     │end procedure          │    │  
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 

The scope for the procedure foo now looks like:

┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐ 
│      ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐     ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░░░░░░░░░░│    │  
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░░░░░░░░░░│    │  
│      │include myinc.e │     │global procedure foo() │    │ 
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░░░░░░░░░░│    │  
│      │░░░░░░░░░░░░░░░░│     │end procedure          │    │  
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░░░░░░░░░░│    │  
│      │░░░░░░░░░░░░░░░░│     │░░░░░░░░░░░░░░░░░░░░░░░│    │  
│      └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘     └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘    │ 
└╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘ 
 
       myprog.ex             myinc.e 
 

Like all routines, the procedure foo is visible everywhere because scope "floods" the available space.


I have to get at least a grade of 1/10 from Derek Parnell to declare success. So I will keep trying.

_tom

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu