zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH 0/2] persistent locals (sort of)
Date: Tue, 5 Sep 2023 22:04:48 -0700	[thread overview]
Message-ID: <CAH+w=7Ycj3i3oktyVPbSC_sYacOZz8kO48mPSno8ECJeKY61EA@mail.gmail.com> (raw)

This is an idea that was seeded by the zsh-users question from Budi
about "plain variable which is retaining its value all the time".
Diffs to follow in two parts:

Part 1:  There doesn't seem to be any reason a named reference should
not be declared "hideval" (-H).  This just means that if it gets
dumped out in a "typeset -p" output, you don't see what it refers to.
A minor drawback is that this is also what an undefined "placeholder"
named reference looks like.  This bit isn't essential to the scheme,
see below.

Part 2:  A new module zsh/param/persistent.  It introduces a builtin
"persistent" which acts like "local" except that when run again at the
next call to the function, it restores the value from the previous
call to the function (actually, to any function having the same name).
Any assignment made in the arguments to "persistent" is ignored, so
this looks very much like "static" in C, except that the name is
visible to nested scopes as usual.

This should probably be considered a proof of concept at the moment.
The way it actually works:

A namespace prefix is first generated based on the function name, so
if the function is "foo" the namespace is ".persistent_foo."  This
means it can only be used in functions that have names in the form of
simple identifiers; it'll produce an error if used outside a function
or in an anonymous function or in any function with dots, hyphens,
slashes, etc. in the name.  This could be solved with some kind of
function-name hashing, I suppose.

The argument list is then walked and split into name and value at "="
as with typeset.  Because this is a builtin, there can't be array
assignments etc., which is a second drawback.  Every name is prefixed
with the namespace and checked for existence; iff it doesn't yet exist
in the namespace, it's created with the specified value if any.
Finally, and always, all the original names are created as named
references to their counterpart in the namespace, using the hideval
option from Part 1.

The end result is that the calling function always has named
references to the same hidden global variables.  Only those references
are re-created on every re-entry.

A further change (or option) that could make this even more
"static"-like would be to create the named references using "private".
In fact I suppose this could even be merged into the "private" module.
I debated copying that module's hack for creating a keyword, but
decided against it.


             reply	other threads:[~2023-09-06  5:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-06  5:04 Bart Schaefer [this message]
2023-09-06 23:38 ` Bart Schaefer

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=7Ycj3i3oktyVPbSC_sYacOZz8kO48mPSno8ECJeKY61EA@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).