From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Up-scope named references, vs. ksh
Date: Fri, 1 Mar 2024 18:22:38 +0000 [thread overview]
Message-ID: <20240301182238.tpyajwblbam5bxw7@chazelas.org> (raw)
In-Reply-To: <CAH+w=7aGtWkXFYXQRnL808CscE0=CsAvA3zpoUK6LcSzk5JEww@mail.gmail.com>
2024-02-28 21:16:13 -0800, Bart Schaefer:
[...]
> That's where this whole discussion started: It's not good enough to
> "record that it's to be global" because if a local of the same name is
> later declared, there is no way to insert a new global "above" that,
> 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). The
> workaround in shell code is to start with "typeset -g a" (using your
> example code) to initialize the top stack frame. The workaround in
> underlying C code would be to have "typeset -n r1=a" (again your
> example) implicitly create the global $a as early as possible.
[...]
Couldn't typeset -n ref=var, when it finds that var is not set
create one in global scope with a "unset-but-referenced" flag
and maybe a reference count associated with it and that can be
unset but not removed until the reference count reaches 0 when
no nameref point to it?
Another (similar) problem:
Imagine a function meant to create a variable whose name is
passed as argument *as a scalar*:
$ ./Src/zsh -c 'f() { typeset -n ref=$1; local var=2; ref=3; }; typeset -A var; f var; echo $var'
f: var: attempt to set slice of associative array
Doesn't work if the variable was previously set as a hash.
Work around would be to unset it first but:
$ ./Src/zsh -c 'f() { typeset -n ref=$1; unset ref; local var=2; ref=3; }; typeset -A var; f var; echo $var'
<empty>
$ref ended up pointing to the local var again instead of the one
in global scope it was intended to reference.
Comparing with ksh93:
$ ksh -c 'function f { typeset -n ref=$1; typeset var=2; ref=3; }; typeset -A var=([a]=b); f var; typeset -p var'
typeset -A var=([0]=3 [a]=b)
$ ksh -c 'function f { typeset -n ref=$1; unset ref; typeset var=2; ref=3; }; typeset -A var=([a]=b); f var; typeset -p var'
var=3
--
Stephane
next prev parent reply other threads:[~2024-03-01 18:23 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-06 2:21 [PATCH 1/3]: Add named references Bart Schaefer
2023-02-08 3:45 ` Oliver Kiddle
2023-02-08 4:59 ` Bart Schaefer
2023-02-08 23:16 ` Bart Schaefer
2023-02-09 0:47 ` Oliver Kiddle
2023-02-09 2:01 ` Oliver Kiddle
2023-02-09 5:45 ` Bart Schaefer
2023-02-09 4:49 ` Bart Schaefer
2023-02-09 20:49 ` Oliver Kiddle
2023-02-09 23:07 ` Bart Schaefer
2023-02-11 3:04 ` Bart Schaefer
2023-02-11 3:55 ` Bart Schaefer
2023-02-11 5:36 ` Speaking of dangerous referents Bart Schaefer
2023-02-12 8:00 ` Oliver Kiddle
2023-02-12 8:34 ` Bart Schaefer
2023-02-11 7:02 ` [PATCH 1/3]: Add named references Oliver Kiddle
2023-02-11 7:45 ` Bart Schaefer
2023-02-11 23:43 ` Bart Schaefer
2023-02-11 23:45 ` Bart Schaefer
2023-02-12 7:38 ` Oliver Kiddle
2024-02-11 7:00 ` Stephane Chazelas
2024-02-11 16:14 ` Bart Schaefer
2024-02-11 16:42 ` Bart Schaefer
2024-02-18 3:26 ` Up-scope named references, vs. ksh Bart Schaefer
2024-02-20 21:05 ` Stephane Chazelas
2024-02-20 22:30 ` Bart Schaefer
2024-02-21 20:12 ` Stephane Chazelas
2024-02-29 5:16 ` Bart Schaefer
2024-03-01 18:22 ` Stephane Chazelas [this message]
2024-03-01 20:34 ` Bart Schaefer
2024-03-02 7:29 ` Bart Schaefer
2024-03-02 23:55 ` [PATCH] "typeset -nu" (was Re: Up-scope named references, vs. ksh) Bart Schaefer
2024-03-01 23:28 ` Up-scope named references, vs. ksh Bart Schaefer
2024-03-03 13:44 ` Stephane Chazelas
2024-03-03 19:04 ` Bart Schaefer
2024-03-03 20:27 ` Stephane Chazelas
2024-03-03 22:58 ` Bart Schaefer
2024-03-04 19:59 ` Stephane Chazelas
2024-03-05 1:05 ` Oliver Kiddle
2024-03-05 2:53 ` Bart Schaefer
2024-03-05 5:43 ` Mikael Magnusson
2024-03-05 6:30 ` Stephane Chazelas
2024-03-06 5:04 ` [PATCH] local vs. nameref scoping (was Re: Up-scope named references, vs. ksh) Bart Schaefer
2023-02-12 9:02 ` [PATCH 1/3]: Add named references Oliver Kiddle
2023-02-12 18:59 ` Bart Schaefer
2023-02-12 19:45 ` PM_* flags in zsh.h (was Re: [PATCH 1/3]: Add named references) Bart Schaefer
2023-02-12 21:01 ` Oliver Kiddle
2023-02-12 22:54 ` [PATCH 1/3]: Add named references Oliver Kiddle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240301182238.tpyajwblbam5bxw7@chazelas.org \
--to=stephane@chazelas.org \
--cc=schaefer@brasslantern.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).