Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: expanding nameref target?

On Tue, Feb 28, 2023 at 12:47 AM Oliver Kiddle <opk@xxxxxxx> wrote:
> Bart Schaefer wrote:
> > In ksh emulation (and history disabled), I have this working:
> >
> > % typeset -n foo=bar
> > % echo ${!foo}
> > bar
> Supporting ${!foo} is useful for ksh (and bash) compatibility so that
> sounds good. There are also other forms such as ${!name[@]}.

There are some significant differences between bash and ksh regarding
${!...}, I have a patch for subst.c that documents this in comments.
The simplest one is that ${!scalar} in bash works like ${(P)scalar} in
zsh, whereas in ksh it just substitutes the parameter's own name.

We already supported ksh ${!foo[@]} for associative arrays, I bolted
nameref handling into that code path (in addition to the zsh flags
path).  However, that's explicitly (and commented-ly) enabled only in
ksh emulation.  We don't support fetching the array of (normal) array
indexes anywhere, I haven't tried to add that.

> > However, that's pretty clumsy because of history expansion, so I'm
> > proposing the following for zsh native mode:
> >
> > % typeset -n foo=bar
> > % echo ${(?)foo}
> I would question whether it's really sufficiently useful to warrant
> that.

It does seem unlikely to be something wanted during an interactive
session.  How about if I leave it with just ${(!)foo} instead of
consuming (?) as well?

> I'm inclined to suspect that anyone who thinks they need ${!foo}
> has probably missed the point of namerefs. Unless they are writing
> something that is meta in nature to begin with

I presumed Mikael had a use-case or he wouldn't have asked about it.
It seems odd to have it available but only usable in ksh mode (e.g.,
${!...} is historically a synonym for ${(k)...}, but that doesn't make
sense for namerefs).

> > As an extension (more by accident than by design but possibly useful):
> >
> > % echo ${(?)options}
> > zsh/parameter
> Doesn't that lead to a conflict for a nameref defined in a module - do
> you get the module name or the target of the nameref?

You get the module until the feature is loaded, and then you get the
target of the nameref.

Messages sorted by: Reverse Date, Date, Thread, Author