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

Re: Up-scope named references, vs. ksh



On Wed, Feb 28, 2024 at 9:16 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> it's akin to attempting to allocate new space in your caller's stack
> frame in C (except there's one stack for each parameter name).

On Fri, Mar 1, 2024 at 12:34 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> zsh uses dynamic scoping, and ksh also has special handling of
> "typeset -n ref=$1" (and other positionals) that is simply never going
> to happen in zsh.

How about this:  Instead of magically turning a positional into a
reference into the surrounding scope, let's make it explicit.

I propose that when the -u flag is combined with the -n flag, the
named reference refers to the enclosing scope, even if there's a local
of that name already in scope.  Otherwise (without -u) normal dynamic
scoping applies.  So in an example like this:

y () {
  typeset $1=Y
  typeset -nu upref=$1
  upref=UP
  echo In y:
  typeset -p $1
}
z () {
  local var
  y var
  echo In z:
  typeset -p var
}

The output would be:

In y:
typeset var=Y
In z:
typeset var=UP

However, there's no good way around the stack problem, so removing the
declaration in z --

z () {
  y var
  echo In z:
  typeset -p var
}

-- will cause an error at the "upref=UP" assignment in y.  Or that
assignment could just silently fail, which already happens in a couple
of existing tests.  The decision on that probably rests on what to do
with prefix assignments, e.g., if it's an error then

upref=UP /bin/echo bad

would print a message and not execute at all.  If instead the
assignment does nothing but set $?, then /bin/echo will execute and
its status will determine $?.




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