zsh-workers
 help / color / mirror / code / Atom feed
* a few question about named reference
@ 2023-07-13  4:05 Jun T
  2023-07-13  6:12 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Jun T @ 2023-07-13  4:05 UTC (permalink / raw)
  To: zsh-workers


[1] what does 'typeset -n ref' do when a variable 'ref' already exists?

(1a) removing a named reference

In the section NAMED REFERENCE in zshparam(1), 

       ... so  to  remove  a
       named reference, use either `unset -n pname' or one of:
              typeset -n pname
              typeset +n pname
       followed by
              unset pname

But:
% i=10
% typeset -n pname=i
% typeset -p pname
typeset -n pname=i
# now try to remove pname
% typeset -n pname     # this does nothing
% typeset -p pname
typeset -n pname=i     # still points to i
% unset pname
% typeset -p i pname
typeset: no such variable: i
typeset -n pname=i

The following works:
% typeset -n pname=    # reset to empty
% unset pname
% typeset -p pname
typeset: no such variable: pname

Just fix the document?

(1b) converting a scalar into named reference

In the first post by Bart about the named reference (worker/51360⁩):

> One difference from ksh93 is that you can't convert scalars into
> namerefs

But it seems we can convert a scalar into nameref:
% i=10
% ref=i
% typeset -n ref
% echo $ref
10

I have no idea whether the conversion needs be prohibited or not.


[2] readonly named reference

% typeset -nr ref=i
% typeset -n +r ref
typeset: no other attributes allowed with -n
% typeset +n ref
typeset: ref: read-only reference
% unset -n ref
zsh: read-only variable: ref

So we can't remove the readonly attribute (and will not be able
to remove/unset it). Is this intentional?


[3] can named reference be in 'unset' state?

If typeset_to_unset is on, creating namerefs with and without =''
have some difference:

% setopt typesettounset
% typeset -n r1 r2=
% typeset -p r1 r2
typeset -n r1
typeset -n r2=''
% echo ${(\!)r1-unset}
unset

So it seems r1 is 'unset'. But:

% echo ${+r1}        # or just "echo $r1"
1
% typeset -p r1
typeset -n r1=''     # now it is set to ''

It seems resolve_nameref() in params.c resets the PM_UNSET bit.
I feel it would be simpler to ignore typesettounset when creating
a nameref.



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

* Re: a few question about named reference
  2023-07-13  4:05 a few question about named reference Jun T
@ 2023-07-13  6:12 ` Bart Schaefer
  2023-07-16  5:09   ` Jun T
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2023-07-13  6:12 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-workers

On Wed, Jul 12, 2023 at 9:06 PM Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>
> [1] what does 'typeset -n ref' do when a variable 'ref' already exists?

As of workers/51402, it does the same thing as
  ref=xx
  typeset -n ref

So ...

> (1a) removing a named reference
>
>        ... so  to  remove  a
>        named reference, use either `unset -n pname' or one of:
>               typeset -n pname
>               typeset +n pname
>        followed by
>               unset pname
>
> But:
> % typeset -n pname     # this does nothing
> % typeset -p pname
> typeset -n pname=i     # still points to i
[...]
>
> Just fix the document?

Yep, documentation dates from the previous patches.  Need to update.

> (1b) converting a scalar into named reference
>
> In the first post by Bart about the named reference (worker/51360⁩):
>
> > One difference from ksh93 is that you can't convert scalars into
> > namerefs
>
> But it seems we can convert a scalar into nameref:

That also changed in 51402, from the commit log:

    * Ksh-style "foo=bar; typeset -n foo" creates foo=bar reference

> [2] readonly named reference
>
> So we can't remove the readonly attribute (and will not be able
> to remove/unset it). Is this intentional?

Yes, that's how it works in ksh.

> [3] can named reference be in 'unset' state?
>
> It seems resolve_nameref() in params.c resets the PM_UNSET bit.
> I feel it would be simpler to ignore typesettounset when creating
> a nameref.

That will need to be looked at.  Thanks for noticing.


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

* Re: a few question about named reference
  2023-07-13  6:12 ` Bart Schaefer
@ 2023-07-16  5:09   ` Jun T
  2023-07-16 17:15     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Jun T @ 2023-07-16  5:09 UTC (permalink / raw)
  To: zsh-workers



> 2023/07/13 15:12、Bart Schaefer <schaefer@brasslantern.com>のメール:
> 
> On Wed, Jul 12, 2023 at 9:06 PM Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>> [2] readonly named reference
>> 
>> So we can't remove the readonly attribute (and will not be able
>> to remove/unset it). Is this intentional?
> 
> Yes, that's how it works in ksh.

With ksh (Version AJM 93u+m/1.0.4 2022-10-22):
$ typeset -n -r ref=i
ksh: typeset: -n cannot be used with other options except -g

But it's OK if it is intentional.

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

* Re: a few question about named reference
  2023-07-16  5:09   ` Jun T
@ 2023-07-16 17:15     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2023-07-16 17:15 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-workers

On Sat, Jul 15, 2023 at 10:10 PM Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>
> >> So we can't remove the readonly attribute (and will not be able
> >> to remove/unset it). Is this intentional?
> >
> > Yes, that's how it works in ksh.
>
> With ksh (Version AJM 93u+m/1.0.4 2022-10-22):
> $ typeset -n -r ref=i
> ksh: typeset: -n cannot be used with other options except -g

Hmm.  My previous reference was ksh88.  The ksh93 I have installed is
a bit older and doesn't have a -g option at all.  Looks like they're
borrowing stuff from zsh, now?

> But it's OK if it is intentional.

Upon reflection and for consistency with other parameter types, it
does seem like +r should be allowed.  There also aren't any test for
readonly namerefs, I should add a couple.


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

end of thread, other threads:[~2023-07-16 17:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-13  4:05 a few question about named reference Jun T
2023-07-13  6:12 ` Bart Schaefer
2023-07-16  5:09   ` Jun T
2023-07-16 17:15     ` 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).