Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Issues with named references
On Sun, May 4, 2025 at 1:23 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>>
>> typeset -n ref
>> x=X y=Y z=Z
>> for ref in x y z; do print $ref; done
>
> Interesting usage but I see this as a way of reinitializing the reference
Of course, but it would look very odd if it was not permitted to
declare a named reference that doesn't (yet) have a referent.
> What I mean is that if you use late initializations and/or references to not yet existing variables, then there are multiple situations in which you can't predict what pname expands to by solely relying on the quote "named references always expand parameters at the scope in which rname existed when ‘typeset -n’ was called".
OK, that's potentially worth clarifying, but it does follow from the
rules for dynamic scoping -- if you assign at scope X and then return
to scope X-1, the parameter you find by dynamic scoping will be the
one available at X-1. Same rule as if both names are declared "local"
and you don't use named references at all. Compare ${(P)name}.
Regarding clarification, in the recent PATCH thread, I wrote:
>
> When the reference is finally assigned, it refers to the same variable
> until that variable goes out of scope. However, that does not change
> the assigned value in the calling function, so when the reference is
> accessed again in the calling scope it finds the corresponding name
> there.
>
> This behavior is required by K01 test "Global variable is a reference,
> warning" and it's not clear that it's wrong -- if this were an
> ordinary parameter, it would remain set in the calling function unless
> explicitly unset. However, an alternate behavior, such as making
> lines h:3: and g:3: behave the same as g:2: and h:2:, is possible.
I'm becoming convinced that the current behavior is correct and the
latter behavior would be wrong, because the latter behavior would
preclude creating a function whose purpose is to set the target of a
named reference declared in the calling function.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author