zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Up-scope named references, vs. ksh
Date: Sun, 3 Mar 2024 14:58:04 -0800	[thread overview]
Message-ID: <CAH+w=7ZZrCh23Q3kzJ3K-TtS1ULnWyR-Cpj_ymSvEgejpvtDgw@mail.gmail.com> (raw)
In-Reply-To: <20240303202756.7axmzy6gkohza2ra@chazelas.org>

On Sun, Mar 3, 2024 at 12:27 PM Stephane Chazelas <stephane@chazelas.org> wrote:
>
> 2024-03-03 11:04:03 -0800, Bart Schaefer:
> >
> > If the parameter is magically created "in advance" (at the point of
> > "typeset -n") then it only works for global scalars
>
> Why would we need it for any other scope? If the parameter is
> not found in any scope, then it must be created in global scope.
>
> That's what "read var" does.

The word "scalar" is just as important in that sentence as the word
"global" -- "read" and "zstat" can specify whether the variable is
scalar or array, you can't do that with a nameref.  Furthermore, if
there's a local in scope at the point where "read var" is called, it
assigns to the local, even if the local is unset.  The scope is
determined at the point of assignment.

Locals consume the space allocated in the parameter table for the
corresponding name.  This is a longstanding, conscious decision in the
parameter implementation, I even quoted a code comment to that effect
in the thread about unsetting tied parameters.  With -u, a nameref can
skip over one local, but it can't "make room" for a global that didn't
already exist. I've tried several different ways to explain this, so
I'm just going to ignore this going forward.

> Many if not most of the usages of
> namerefs I've come across were to implement "read"-like commands
> that return or can return something in a variable whose name is given by the
> caller (and that can create the variable if needed).

There's nothing in the current implementation that prevents you from
doing that if you choose your local names appropriately (and if
nothing above you in the call chain hasn't already co-opted the name).

> Where an approximation of what I'm suggesting is implemented in
> zsh seems to work fine (with your patch applied).

But that approach doesn't work in the general case!  If you move
  typeset value=$2
to above the [[ -v $1 ]], then it either bombs, or assigns to the
"wrong" $value.

There's no way to magically get this right in the internal
implementation, it has to be handled in shell code.

> It segfaults with zsh -x though

I'll have a look at that later, thanks.

> > If a function wants to be able to actually add things to its calling
> > scope without fear of name clashes, it has to use unique local names
> > (e.g., a namespace).  But with dynamic scoping it remains the case
> > that you can't add a name to the global scope if any function in the
> > call chain has pre-empted that name -- named references don't change
> > this basic rule.
>
> I really don't follow, dynamic scoping should make things easier
> here.

Consider the case where your function is called 2 or more levels
removed from the global scope.

> but having namerefs that can't create globals seems like a very
> severe limitations to me

It's the same limitation as
() {
  local foo
  unset foo
  () { foo=bar }
}
which despite the appearance of the assignment in the nested function,
doesn't create a global foo, it resurrects the caller's local foo.
Ksh has static local scope (and an entirely different internal
representation of parameters) so the local foo makes no difference to
the nested function.


  reply	other threads:[~2024-03-03 22:58 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
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 [this message]
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='CAH+w=7ZZrCh23Q3kzJ3K-TtS1ULnWyR-Cpj_ymSvEgejpvtDgw@mail.gmail.com' \
    --to=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).