When a function is exited and the scope of named reference is updated because it referred to a local variable of the exited function, should its scope be set to the (new) current scope (i.e., the scope of the calling function) or should it be set all the way up to the scope of the next enclosing rname variable or the global scope if no such variable exists?
The current implementation does the former. I think that the latter would be better.
Consider the following function:
function f1() {
typeset args=($@);
typeset -n ref;
typeset var=$0;
function f2() {
function f3() {
if (($args[(I)f3])); then typeset var=$0; fi;
ref=var;
echo "$0: ref=$ref";
}
f3;
echo "$0: ref=$ref";
if (($args[(I)g2])); then typeset var=$0; fi;
echo "$0: ref=$ref";
function g3() {
typeset var=$0;
echo "$0: ref=$ref";
}
g3;
echo "$0: ref=$ref";
}
f2;
}
Below are the results for 4 different calls.
|--------------------------+--------------------------|
| No var defined in f3 | Local var defined in f3 |
|--------------------------+--------------------------|
| % f1 | % f1 f3 |
| f3: ref=f1 | f3: ref=f3 |
| f2: ref=f1 | f2: ref=f1 |
| f2: ref=f1 | f2: ref=f1 |
| g3: ref=f1 | g3: ref=g3 |
| f2: ref=f1 | f2: ref=f1 |
|--------------------------+--------------------------|
| % f1 g2 | % f1 f3 g2 |
| f3: ref=f1 | f3: ref=f3 |
| f2: ref=f1 | f2: ref=f1 |
| f2: ref=f1 | f2: ref=f2 |
| g3: ref=f1 | g3: ref=f2 |
| f2: ref=f1 | f2: ref=f2 |
|--------------------------+--------------------------|
Even though the only difference between the calls in the two columns is that a local variable var is additionally defined in f3 in the calls in the second column, it changes how ref behaves in f2 and g3.
In order to understand how ref behaves in f2 and g3 after f3 exited, you have not only to know with which variable name ref is initialized and where variables with that name currently exist but you also have to know whether a variable with that name existed within f3***.
If instead the scope of updated named references was set all the way up, then all lines in the examples above, except for the ones printed by f3, would be the same. This has the benefit that to understand what ref refers to at any point in the program you only need to know with what variable name ref is initialized and where variables with that name currently exist. The circumstances under which ref was initialized and whether other variables with the specified name existed in now closed scopes doesn't matter.
Given this, it seems to me that "all the way up" is preferable to "current scope" because it makes it much easier to understand what a named reference is referring to.
Philippe
*** Actually it's even worse, you also have to know whether the variable var in f3 was defined before or after ref was initialized.