help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH 1/3]: Add named references
Date: Sun, 12 Feb 2023 08:38:59 +0100	[thread overview]
Message-ID: <90908-1676187539.552165@4_cC.JL2y.vDqw> (raw)
In-Reply-To: <CAH+w=7YN_i9KxgZFFn1zNe98pT0KmErdpHNZr_oAWf5CpwsgSg@mail.gmail.com>

Bart Schaefer wrote:
> > > I'd be
> > > fine with the reference becoming unset at the time of the return if it
> > > refers to a variable that is going out of scope. Can that be done as
> > > part of the same code that drops the local variables?
> That's actually a little startling given dynamic scoping.  It leads to this:

While I said "unset", what I actually meant was only dropping the value
part of the reference - returning it to the `typeset -n ref` state.


> Instead let's think for a moment about how the dynamic scoping works.
> "ref" has two properties:  The locallevel at which it was declared,
> and the locallevel at which it prefers to find a referent.  Any time
> you use $ref (or assign to ref) the search starts at the current
> locallevel and proceeds upward until either the preferred level is
> reached or the "topmost" defined parameter is found, whichever comes
> first.

You've persuaded me. It is consistent with dynamic scoping in general.
Perhaps it has further convinced me (not that it needed to) that lexical
scoping is generally better.

Also when following and considering your examples, the following
scenario occurred to me. I can't see a reason to object to this
behaviour and some of the ideas I had in mind would mess with it. Note
that the reference starts out pointing to an unset variable:

  bar() {
    typeset var=hello
    echo $ref
  foo() {
    typeset -n ref=var
    echo $ref
  unset var

It'd be surprising if calling bar somehow lost the value of the


  parent reply	other threads:[~2023-02-12  7:39 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-06  2:21 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 [this message]
2023-02-12  9:02             ` 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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=90908-1676187539.552165@4_cC.JL2y.vDqw \
    --to=opk@zsh.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \


* 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


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).