Original date:2017-04-20 13:14:01 Edited by: _tom Subject: Re: rant OE scope
forked out of 500 Rosetta

[quote petelomax] I wonder if "line of sight" might be a more appropriate metaphor.


Paradoxically, scopes can be difficult to describe and/or understand, precisely because they are inherently obvious.

Pete [/quote]

Physical models don't quite work with scope. But, the "line-of-sight" model has promise.


When you write a computer program it looks like this to you:

atom x = 0 
atom sum = 0 
while 1 do 
	x += 1 
	sum += x 
	if x > 5 then exit end if 
end while 
? sum 
--> 21 

When you look a a landscape what you see is something like this:

...................................   horizon (end of space) 
				     / \ 
		              / \ 
			   / \ 
			  /   \ 
			 /     \ 
			/       \ 
 / \   you, looking at a landscape full of trees 

What the same program looks like to an interpreter is:

-- look towards the horizon 
                                                                 ^   all the way to end of space 
			 ? sum                                   │  
		     end while                                   │ 
                if x>5 then exit end if                          │  
   	    sum += x                                             │   
        x += 1                                                   │ 
     while 1 do                                                  │ 
  atom sum = 0                                                   │ 
atom x = 0                            -- you can "see" x here ---┘             
                            "x" does no exist yet, you can't see it, out-of-scope 
--start here 

An identifier does not exist until you make a declaration; if it does not exist you can not see it.

If the Interpreter can "see" it then it is "ok", things are in-scope and things work. If the Interpreter can not "see" an identifier, then it is out-of-scope and it crashes.

The "line-of-sight" model should work for a lot of people (if their mind is flexible enough to to flip things about).


Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu