[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Mar 21 19:24:38 1995 
Subject:Lexical Scoping - possibly ridiculous idea 
From:James Anderson 
Volume-ID:950321.01 

The following idea may well be ridiculous so you might want to delete 
this message now.

The scoping rules in Pop are interesting, to say the least. Here is a
suggestion on how things could be done differently in some language, not
necessarily Pop. The essence of the idea is that in most languages variables
default to global scope and variabledeclarations limit scope. We could do the
opposite. That is, have default rules defining local scope and have declarations
expand the scope of variables.What follows is a first stab at the default rules.
I am sure this suggestion is notworkable as it stands, but what are news groups 
for? :-)

1) All variables default to lexical scope.
   (Pop seems to be drifting this way.)

2) The initial value of a variable is set to the value of the first
   same-named variable in successively less-nested scopes. (That is,
   global import of the initial value is the default.)

3) All varaibles default to having scope of their current lexical block.
   (We would have to tighten up definitions of lexical blocks in Pop.)

4) Default definitions can be over written by explict definitions of
   globality. (See the following examples.)

Example 1
=========

define foo(v1) -> v2; ;;; 1
    for v1 to 10 do   ;;; 2
        ...
    endfor;
    v1 -> v2;         ;;; 3
enddefine;

1) Initial instantiation of variables "v1", "v2";
2) New instance of variable called "v1", shares initial value with "v1"
   at (1).
3) Initial instantiation of "v1" at (1) assigned to "v2".

Example 2
=========

define foo(v1) -> v2; ;;; 1
    lvars v1;         ;;; 2
    for v1 to 10 do   ;;; 3
         ...
    endfor;
    v1 -> v2;
enddefine;

1) Initial instantiation of variables "v1", "v2".
2) Lexically nested versions of "v1" exported to current block.
3) Same computation performed as in Example 1, but with "v1" at (1) not
   "v1" at (2).
4) Value of "v1" after "for" loop assigned to "v2".

Conclusion
==========
An advantage of this proposal for Pop might be that existing code with explicit
variable declarations might be made to work as now, but the absence of variable
declarations brings about tight block-binding of lexical variables. In
other words, "expert" code in the libraries might be unchanged, but code
produced by "novices" would probably fall over. Mind you, most novice code is
ususally short lived, and so are novices! Either they give up or they
become experts. (-: Perhaps there is a third alternative? Let me see. :-)

James Anderson