[Swift-devel] restart variable scoping.

Ben Clifford benc at hawaga.org.uk
Thu May 22 08:09:16 CDT 2008


wrt to the on-going activity around bug NNN about restarts...

if it is desirable to label restarts based on variable name, rather than 
by the mapped filename, then:

the naive approach of storing the variable name as restart identifier:

 * works for global variables
 * does not work for non-global variables, because the same variable name 
   can be used in different scopes, and the same code block can be invoked 
   multiple times (in procedures and in for loops, for example)

Some kind of scope identifier on the front might help here. In:

  (file t) p() {
     file q=foo();
     t=qux(q);
  }

  a=p();    // l1
  b=p();    // l2

the variable 'q' is used twice, once in the call at l1, and once in the 
call at l2.

So one might use 'l1' and 'l2', the locations in the source file, as scope 
identifiers, giving restart identifiers of l1.q and l2.q.

In a foreach loop, such as:

foreach a,i in array {  // l3
   out[i] = p(array); // l4
}

we might then scope the various instances of q as:

   l3-iteration1.l4.q
   l3-iteration2.l4.q
   l3-iteration3.l4.q
   [...]

with scope labelling being hierarchical.

The scope identifier is:

  the containing scope identifier
   +
  whatever is needed to identify the new child scope within the containing 
  scope

So: entering a foreach loop block appends the iteration identifier to the 
current scope; making a function call appends the function call source 
location (or equivalent) to the current scope.

This is mostly based around me thinking about a SwiftScript level of 
thinking, rather than a Karajan level of thinking - it may be that this 
is not easy to do at the karajan level (indeed, it may be that this 
doesn't work at all anyway). But it seemed worth sending anyway.

-- 



More information about the Swift-devel mailing list