Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: All the way up or current scope



On Tue, May 20, 2025 at 6:00 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> In the first example it only works because the "for" construct is used. If that's the only reason we should support references to nested variables, it sounds like a very high price for a very specific and most likely very rare use case.

I don't really see either side of this in terms of a "price", but I
may be overestimating the uses.  And the "for" implementation is a
special case anyway, so that may avail of a different solution.

> Now, regarding all the way up, since my previous arguments couldn't convince you, here is one of your own, namely the lack of self-reference detection.
>
>> in fact, because (with your "loose"  experiment patch) a
>> reference can "float" back into its own scope, it's trivially easy to
>> cause a reference to loop back to itself.
>
> What example did you have in mind?

The two tests in K01 that use the parameter name "myself".  With
"sticky" references, those tests pass.  With "loose", they run to
"math recursion limit exceeded".

> I noticed that in the following example the loop is detected only if the outer ref1 is commented out.
>
> () {
>   typeset ref1=outer
>   () {
>     typeset -n ref2=ref1
>     echo "ref1=$ref1 ref2=$ref2"
>     typeset -n ref1=ref2
>     echo "ref1=$ref1 ref2=$ref2"
>   }
> }

That's ... not a loop.  If it were an undetected loop, the shell would
hang until it ran out of stack and then crash.  It's a chain,
ref1(inner) to ref2(inner) to ref1(outer).

> () {
>   typeset -n ref1=ref2
>   typeset -n ref2
>   () {
>     () {
>       typeset ref1=inner;
>       ref2=ref1
>       echo "ref1=$ref1 ref2=$ref2"
>     }
>     echo "ref1=$ref1 ref2=$ref2"
>   }
> }

That's not a loop either.  The very first ref1=ref2 creates a
reference to a nonexistent but otherwise non-reference global ref2,
and by the end you have a chain ref2 to ref1 to nothing, even though
you can't see it unless you actually instantiate a (normal scalar)
ref2 in global scope.  I admit it's a pretty weird case.




Messages sorted by: Reverse Date, Date, Thread, Author