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

Re: nameref binding not finding calling-scope vars?



On 2025-03-31 at 15:00 -0700, Bart Schaefer wrote:
> Attached patch fixes this.  Tests based on this thread included.

These work great, thanks.  The only problem now is figuring out which
approach makes the most sense.  :)

It feels like `local -un var="$1"` makes the most sense, as it is
explicitly stating that the variable should be found externally.

However, if I use `setopt warn_create_global` then the version where
inner() uses `typeset -ga` is the only one which does not issue a
warning.

 A: inner() { local -n var="${1:?}"; var=(alpha beta gamma); }
 B: inner() { local -n var="${1:?}"; typeset -ga var=(alpha beta gamma); }
 C: inner() { local -un var="${1:?}"; var=(alpha beta gamma); }

Of these three, I would expect A to warn under warn_create_global, but
not B or C. In practice, both A and C do.

Is my thinking here Wrong?  It seems like `-un` is being very explicit
that the value should be found in the calling scope, much like the
implicit behaviour of -g when using the nameref, so there's no need for
the warning.

It looks like check_warn_pm tries to avoid warnings for nameref, but of
course that flag isn't set on the basepm which is found by searching, so
doesn't take effect?

> This should work after the attached patch.

Everything I mentioned first time does, so again: thank you.  :)
My grotesquely over-engineered custom completion for a special type now
can set vars other than $reply.

-Phil




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