From: Oliver Kiddle <email@example.com>
To: Bart Schaefer <firstname.lastname@example.org>
Cc: Zsh hackers list <email@example.com>
Subject: Re: [PATCH 1/3]: Add named references
Date: Wed, 08 Feb 2023 04:45:40 +0100 [thread overview]
Message-ID: <67689-1675827940.088548@BxvG.D9_b.7RzI> (raw)
Bart Schaefer wrote:
> When a reference is created, it's necessary to determine the scope in
I've run some tests and this looks good. The main issue I noticed was
with a reference to associative array elements.
typeset -A assoc
typeset -n ref=assoc[other]
This does work if assoc is assigned an initial value.
The error message from, e.g. `typeset -Z2 ref` is "can't change type
of a named reference". However, this is normally an attempt to change
the type of the target of a reference (-n is not specified). It works
fine for a scalar. So perhaps the error message should complain about
changing the type of [associative] array elements instead.
One ksh feature which this doesn't cover is the special behaviour in ksh
that `typeset -n ref=$1` will use the calling function scope to resolve
the target. This is necessary with the lexical (private) scoping of
ksh93 functions. The positional parameters are special-cased which I
find rather ugly. You could overload -u or -U for indicating Tcl-like
uplevel. The trouble with uplevel is that a called function can't know
how many levels up to specify if people are allowed to write wrappers.
I think a better solution is to take some syntax that isn't a valid
variable name. e.g typeset -n ref=\&1 Repeated use of an initial & would
indicate where another level of scope should be ascended. Wrapper
functions then don't need their own nameref and can just pass, e.g.
\&var as the variable parameter.
> One difference from ksh93 is that you can't convert scalars into
> namerefs, e.g., both with this and in ksh93:
That seems reasonable enough.
> Named references can't have any other attributes, and attempting to
> add an attribute to a named reference generates a warning. However,
I can't see an actual conflict in permitting -r, making the reference
readonly not the target variable.
> With respect to zsh/param/private parameters, it's presently
> prohibited to have a "public" reference to a private parameter. It
> might be possible to change this if someone can think of a use case.
To a user it would seem like a fairly arbitrary restriction.
Coming back to the uplevel question, for private to be more useful it'd
be really great to be able to have a private reference to a private
variable from the calling function scope. I'm not sure how easy that
even is with the current implementation of private.
> There is one glitch: if the reference is created before the parameter
> is declared private, and the private local is NOT hiding something in
> an outer scope, then the reference does not retroactively generate an
> error and assignments to the reference are silent no-ops.
Not sure I follow. Sounds like something that ought to be fixed.
> I also removed "kz" from TYPESET_OPTSTR in zsh.h -- as far as I can
> tell they should never have been there in the first place.
typeset -fu is like autoload. I think it was intentional.
Note that recent bash also has namerefs so should perhaps be compared.
They only resolve in local scope and don't include the local level with
PS. The basic approach is much the same as that which was rejected many
years back with consensus at that time being that a C pointer reference
should be used. I did make a start at the time using reference counting
but real life stuff got in the way and it was never completed. Zsh's
code does a lot of deleting and recreating parameter structures which is
problematic with that approach. I do still have an old patch that last
got rebased as recently as only 7 years ago if you're interested. Aside
from not working with array subscripts, I think I also concluded that it
wouldn't work for private variables because of their somewhat too clever
next prev parent reply other threads:[~2023-02-08 3:46 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 [this message]
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
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
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 \
* 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).