Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Up-scope named references, vs. ksh
- X-seq: zsh-workers 52648
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: Up-scope named references, vs. ksh
- Date: Fri, 1 Mar 2024 23:29:35 -0800
- Archived-at: <https://zsh.org/workers/52648>
- In-reply-to: <CAH+w=7YAgtUDrrgrDedVpq478ow7S=Renj+rjs0ZF6ZFTqP5OQ@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <CAH+w=7YVJO-HkneMpnfBbqBztPaXdXTD=mo-vHbdUW00TiFVBQ@mail.gmail.com> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <CAH+w=7aKwKhdXjPo2FQG1GqjHQzX=5to_m6kZeL-UFfQh_XMtw@mail.gmail.com> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> <CAH+w=7ateaqX5azdifTaFpJT6sX-fVhnEazgeYYXSWtJY8EQTw@mail.gmail.com> <CAH+w=7akb334QvsofyMLtc7_091bmP=omjAGOZSc8eH8FWuTqQ@mail.gmail.com> <20240220210553.g6imt3op6zahz4pa@chazelas.org> <CAH+w=7bucofSovKLc0kJeMT3RcKUc8ydhFSaGQqMALvJ_0S21Q@mail.gmail.com> <20240221201215.anpjcfav6na55gg6@chazelas.org> <CAH+w=7aGtWkXFYXQRnL808CscE0=CsAvA3zpoUK6LcSzk5JEww@mail.gmail.com> <20240301182238.tpyajwblbam5bxw7@chazelas.org> <CAH+w=7YAgtUDrrgrDedVpq478ow7S=Renj+rjs0ZF6ZFTqP5OQ@mail.gmail.com>
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