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

Re: Warning about failed assignments to references to not yet defined variables



On Wed, May 21, 2025 at 1:39 AM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> function () {
>   typeset -nu ref=var
>   typeset var=inner
>   ref=outer
>   echo ref=$ref
> }
> typeset -p var
>
> The assignment ref=outer is supposed to create a global variable named var but it can't because there is a local variable with the same name. Shouldn't this print a warning?

I'm still going to reply further to this on the other thread, probably
tomorrow, but the documentation specifically mentions that your
example is probably wrong:

   To force a named reference to refer to the outer scope, even if a local
   has already been declared, add the -u option when declaring the named
   reference.  In this case RNAME should already exist in the outer scope
   before PNAME is initialized, otherwise the behavior of assignment
   through PNAME is not defined and may change the scope of the reference
   or fail with a status of 1.

Note "In this case RNAME should already exist in the outer scope."  If
you test the result of the ref=outer assignment, you'll see $? == 1.
Behavior is explicitly not defined.




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