zsh-workers
 help / color / mirror / code / Atom feed
* expanding nameref target?
@ 2023-02-21 13:07 Mikael Magnusson
  2023-02-21 15:59 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2023-02-21 13:07 UTC (permalink / raw)
  To: zsh workers

Is there a way to expand a nameref to its target parameter without
removing the nameref attribute with +n first?

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-21 13:07 expanding nameref target? Mikael Magnusson
@ 2023-02-21 15:59 ` Bart Schaefer
  2023-02-28  5:00   ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2023-02-21 15:59 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh workers

On Tue, Feb 21, 2023 at 5:08 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> Is there a way to expand a nameref to its target parameter without
> removing the nameref attribute with +n first?

Not yet.  Still working on that.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-21 15:59 ` Bart Schaefer
@ 2023-02-28  5:00   ` Bart Schaefer
  2023-02-28  8:47     ` Oliver Kiddle
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2023-02-28  5:00 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh workers

On Tue, Feb 21, 2023 at 7:59 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Tue, Feb 21, 2023 at 5:08 AM Mikael Magnusson <mikachu@gmail.com> wrote:
> >
> > Is there a way to expand a nameref to its target parameter without
> > removing the nameref attribute with +n first?
>
> Not yet.  Still working on that.

So ... opinions solicited before I send a patch.

In ksh emulation (and history disabled), I have this working:

% typeset -n foo=bar
% echo ${!foo}
bar

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}
bar

As an extension (more by accident than by design but possibly useful):

% echo ${(?)options}
zsh/parameter

This only works for autoloaded parameters that are not yet loaded.

For similarity with ksh mode, ${(!)foo} is a synonym for (?), but that
means we can't use (!) for anything else.  Is that a problem?

I've also made (?) [or (!)] mutually exclusive with (k) and (v).

Thoughts?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-28  5:00   ` Bart Schaefer
@ 2023-02-28  8:47     ` Oliver Kiddle
  2023-02-28 10:33       ` Mikael Magnusson
  2023-02-28 17:55       ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Oliver Kiddle @ 2023-02-28  8:47 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Mikael Magnusson, zsh workers

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[@]}.

> 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. 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 like the describe-params
function that recently circulated on the -users list. ${!foo} may give
you the name of a variable that is hidden by a local variable so you
don't want to rely on it if you want your code to be robust.

So my view is that I wouldn't bother expending one of the few remaining
flag characters on it.

> 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?

Oliver


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-28  8:47     ` Oliver Kiddle
@ 2023-02-28 10:33       ` Mikael Magnusson
  2023-02-28 18:01         ` Bart Schaefer
  2023-02-28 17:55       ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2023-02-28 10:33 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Bart Schaefer, zsh workers

On 2/28/23, Oliver Kiddle <opk@zsh.org> 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[@]}.
>
>> 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.

What about (PP)? There is some precedent for doubling a flag to
disable its effect. The downside is that (PP) already works and is
treated like (P) but...

> 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 like the describe-params
> function that recently circulated on the -users list. ${!foo} may give
> you the name of a variable that is hidden by a local variable so you
> don't want to rely on it if you want your code to be robust.
>
> So my view is that I wouldn't bother expending one of the few remaining
> flag characters on it.
>
>> 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?

Does a nameref defined in a module before the module is loaded
actually have a target yet?

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-28  8:47     ` Oliver Kiddle
  2023-02-28 10:33       ` Mikael Magnusson
@ 2023-02-28 17:55       ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-02-28 17:55 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Mikael Magnusson, zsh workers

On Tue, Feb 28, 2023 at 12:47 AM Oliver Kiddle <opk@zsh.org> 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: expanding nameref target?
  2023-02-28 10:33       ` Mikael Magnusson
@ 2023-02-28 18:01         ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-02-28 18:01 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Oliver Kiddle, zsh workers

On Tue, Feb 28, 2023 at 2:33 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> What about (PP)? There is some precedent for doubling a flag to
> disable its effect. The downside is that (PP) already works and is
> treated like (P) but...

The other downside is that ${(P)nameref} would not be the "opposite"
of ${(PP)nameref}.  You'd go from two "dereferences" to zero.

Also I'd prefer ${(PP)...} to mean ${(P)${(P)...}} if that were
do-able (tho likely it isn't).


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-02-28 18:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-21 13:07 expanding nameref target? Mikael Magnusson
2023-02-21 15:59 ` Bart Schaefer
2023-02-28  5:00   ` Bart Schaefer
2023-02-28  8:47     ` Oliver Kiddle
2023-02-28 10:33       ` Mikael Magnusson
2023-02-28 18:01         ` Bart Schaefer
2023-02-28 17:55       ` Bart Schaefer

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