zsh-workers
 help / color / mirror / code / Atom feed
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: Wed, 21 Feb 2024 20:12:15 +0000	[thread overview]
Message-ID: <20240221201215.anpjcfav6na55gg6@chazelas.org> (raw)
In-Reply-To: <CAH+w=7bucofSovKLc0kJeMT3RcKUc8ydhFSaGQqMALvJ_0S21Q@mail.gmail.com>

2024-02-20 14:30:56 -0800, Bart Schaefer:
> On Tue, Feb 20, 2024 at 1:05 PM Stephane Chazelas <stephane@chazelas.org> wrote:
> >
> > 2024-02-17 19:26:07 -0800, Bart Schaefer:
> > [...]
> > >  A) Src/zsh -c 'function f { typeset -n ref; ref=$1; typeset var=foo;
> > > ref=X; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"'
> >
> > Note that the ksh93 man page mentions:
> >
> > ksh93> Note  that,  for this to work, the positional parameter must be
> > ksh93> assigned directly to the nameref as part of the declaration command
> > ksh93> [...]   For instance, typeset -n var; var=$1
> > ksh93> won't cross that barrier
> 
> And yet, after replacing ksh2020 with Version AJM 93u+ 2012-08-01

With recent maintained versions with all the bug fixes, it would
look something like:

$ ./arch/linux.i386-64/bin/ksh -c 'echo ${.sh.version}'
Version AJM 93u+m/1.1.0-alpha+b8e3d2a4 2024-02-17

The one on Ubuntu 22.04 has:

$ ksh -c 'echo ${.sh.version}'
Version AJM 93u+m/1.0.0-beta.2 2021-12-17

93u+ 2012-08-01 would be the one from AT&T without the later bug
fixes.

> ksh -c 'function f { typeset -n ref; ref=$1; typeset var=foo;
> ref=X; echo "$ref ${!ref} $var"; }; f var; echo "$var"'
> X var foo
> X
> 
> So it sure looks like the barrier was crossed in a case where the doc
> stated it would not be.

But in that case, you're referencing a variable from the global
scope, not the local scope from a parent function.

[...]
> > The only remaining problem is when the refered variable
> > is not set/declared.
> 
> It IS possible to make that "typeset -g" implicit, but that has other
> potential effects such as creating an (unset) parameter in global
> scope (triggering WARN_CREATE_GLOBAL?).  If the parameter already has
> been declared in a calling function scope, then even with the -g, the
> nameref will point to that one, even if it's more than one level up
> the call chain.  Which ought to be what you expect from dynamic
> scoping.
> 
> > Contrary to mksh or bash, it alread gets this right:
> 
> Yes, as soon as an existing parameter is found the nameref records
> what scope contains that parameter and looks for it there when
> (de)referenced.  The glitchy bit is when there is no existing
> parameter from which to obtain a scope.  I'm reluctant to try to make
> this too magical, which is why I lean toward asking function authors
> to explicitly do the "typeset -g $1" when that's what they mean.
> Could certainly add this to the doc.
[...]

I have no notion of how zsh variables are implemented so I'm
probably talking rubish and it's hard to think this whole thing
through, but it seems to me that for a nameref variable, upon
typeset -n ref=var, zsh should record at that point what scope
or level of the stack the var is to be found and keep that
record for the duration of the scope where that nameref is
defined or until the next typeset -n ref=anything in the same
scope.

Like:
- if var is found (declared) in the current scope, record that
  it's that of that scope that it is to get the value of or set
  or unset (and reset again thereafter)
- if not, repeat for parent scope
- if not found at all, record that it's to be at the global
  scope.

Like in:

f() { typeset a; unset a; g; echo "$a"; }
g() { h; }
h() { 
  typeset b=x
  typeset -n r1=a r2=b r3=c
  typeset a=foo c=bar
  r1=x r2=y r3=z
  unset r{1..3}
  r1=x r2=y r3=z
}
f

r1=x sets the $a of f, r2=y sets the $b of h, and r3=z sets a
global $c both times.

[...]
> So did I.  I await with gritted teeth the first time that Ray forgets
> to declare his loop variable with a nameref of the same name in scope.
[...]

Yes, difficult decision to choose between
- cleaner design, align with mksh
- align with original design in ksh93 and bash which already
  copied it (well, that part).

-- 
Stephane


  reply	other threads:[~2024-02-21 20:12 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 [this message]
2024-02-29  5:16                               ` Bart Schaefer
2024-03-01 18:22                                 ` Stephane Chazelas
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=20240221201215.anpjcfav6na55gg6@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).