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

One more named reference scoping thing



I want to talk about the result of one of my additions to K01nameref
based on Philippe's nr-test script.  Note that the set of functions
has been modified from nr-test to remove everything after test 6 and
add a new last step, which is what's important here.

 e -u 6
0:assignment at different scope than declaration, -u 6
>g:1: rs= - ra= - rs1= - ra1=
>g:2: rs= - ra= - rs1= - ra1=
>h:1: rs= - ra= - rs1= - ra1=
>h:2: rs= - ra= - rs1= - ra1=
>i:1: rs= - ra= - rs1= - ra1=
>i:2: rs=h - ra=h - rs1=h - ra1=h
>j:1: rs=h - ra=h - rs1=h - ra1=h
>j:2: rs=h - ra=h - rs1=h - ra1=h
>i:3: rs=h - ra=h - rs1=h - ra1=h
>k:1: rs=h - ra=h - rs1=h - ra1=h
>k:2: rs=h - ra=h - rs1=h - ra1=h
>h:3: rs=g - ra=g - rs1=g - ra1=g
>k:1: rs=h - ra=h - rs1=h - ra1=h
>k:2: rs=h - ra=h - rs1=h - ra1=h
>g:3: rs=f - ra=f - rs1=f - ra1=f

The results are consistent across each line so I'm going to describe
just one column.

What's going on here is that we have a named reference declared in
function g but not initialized until just before the 2nd line in
function i.  Because of the -u flag, the reference ignores the local
in scope i and binds to the one in scope h.  So far so good.

At the end of function i, after printing the post-j result, there's a
call to k.  This is to force a deeper stack before things unwind back
to function h.  Also so far so good.  Returning from i changes the
scope of the reference to h.

h then prints the state of the reference after the return from i, but
it's a -u reference so it can't refer to the local in the same scope
-- instead it climbs up to g.

Here's where things get interesting.  The only thing that changes the
scope of a named reference is either an assignment or exit from a
scope, and neither of those has happened.  h calls k again -- and now
the referent is no longer in the same scope as the reference, so when
k prints it, it refers to h again.

The question is, is this OK?  Or should return from i have set the
scope all the way to g?  That has different side-effects in other
circumstances.

Lastly k and h both return, setting the scope to g, where -u again
makes it climb up and print f, which is consistent with the foregoing.

Thoughts?




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