zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: Re: "typeset -p" inconsistency
Date: Tue, 1 Nov 2022 12:08:28 -0700	[thread overview]
Message-ID: <CAH+w=7ZqryPVQGXkzPbr_DxkTpEJnGE19CL6iCo3-MEeBui=ug@mail.gmail.com> (raw)
In-Reply-To: <9712ca3f-76f8-3aed-0a40-c6269afb73ad@eastlink.ca>

On Tue, Nov 1, 2022 at 5:40 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2022-10-31 22:00, Bart Schaefer wrote:
> > On Thu, Oct 20, 2022 at 7:25 PM, I wrote:
> >>> typeset -T CDPATH cdpath=(  )
> >>> typeset -aT CDPATH cdpath=(  )
> >> The first one is the (incorrect) assignment for $CDPATH.
> > Nobody called me out on that ... the first assignment is actually just
> > fine.
>
> But the 'typeset - g -aT ... ' remains a bug as you said?  Or at least a
> redundancy?

That's not what I said, in either case.  Again, the -g appears only
when, from inside a function, you ask typeset (by passing the -p
option) to display a command that would create a variable that
persists (is global, hence -g) after the function returns.

The two examples above aren't really "redundant" either.  You asked
typeset to display commands to re-create all the current variables.
The first command is meant to re-create the scalar CDPATH and the
second is meant to re-create the array cdpath.  Because they could
have different attributes (the -U example you trimmed), it's not
redundant to output each of them separately.

The "buglet" is that if you execute both assignments, you end up
assigning all the attributes of either one of the pair to both of the
pair:

% typeset -UT FOO foo=(a b c b a)
% typeset -p foo
typeset -aUT FOO foo=( a b c )
% typeset -p FOO
typeset -UT FOO foo=( a b c )

Both have -U.  Now add -u to just the array:

% typeset -u foo
% typeset -p FOO
typeset -UT FOO foo=( a b c )
% typeset -p foo
typeset -auUT FOO foo=( a b c )

The output is different for each, as it should be ... but if I execute
that first one:

% typeset -auUT FOO foo=( a b c )
% typeset -p FOO
typeset -uUT FOO foo=( a b c )

Oops, we've added -u to FOO as well.  This is an inevitable ambiguity
in the syntax for creating tied variables.

One way to resolve it may be to separate the declaration of tied-ness
from the declaration of other attributes, as PWS mentioned, although I
think there's still ambiguity with respect to in which order the
declarations and the assignments must appear:

% typeset -u FOO
% typeset -T FOO foo=(a b c)
% typeset -p FOO
typeset -T FOO foo=( a b c )

Note -u has been lost from FOO by the subsequent -T declaration.  This
means we can't get away with outputting the assignments for each
variable as we encounter it in the hash table scan, but I'm also not
sure it's sufficient to first scan for and emit all the -T and then
emit all the attribute settings.

There's a comment in the code:

            * We choose to print the value for the array instead of the scalar
            * as scalars can't disambiguate between
            * typeset -T SCALAR array=()
            * and
            * typeset -T SCALAR array=('')

This is an impossible situation for assignment whenever there are
attributes of the scalar that are not in common with the array.

One possibility (which would be a backwards-incompatible change) would
be to treat

typeset -a ... -T FOO foo

as applying all flags to only the array, and

typeset ... -T FOO foo

(without -a) as applying all flags only to the scalar.  That doesn't
fix the assignment problem, though.  Another possibility would be to
allow attribute flags to appear between the scalar name and the array
name, something like:

typeset <attributes for both> -T <attributes for scalar> FOO
<attributes for array> foo

but that breaks the use of our generic option parsing code and still
doesn't solve the assignment ambiguity.


  reply	other threads:[~2022-11-01 19:09 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-20 17:21 var=$( typeset "$1" ) ... not within a function Ray Andrews
2022-10-20 17:25 ` Roman Perepelitsa
2022-10-20 17:51   ` Ray Andrews
2022-10-20 17:54     ` Roman Perepelitsa
2022-10-20 18:38       ` Ray Andrews
2022-10-20 18:44         ` Roman Perepelitsa
2022-10-20 19:15           ` Ray Andrews
2022-10-20 19:35             ` Roman Perepelitsa
2022-10-20 20:48               ` Ray Andrews
2022-10-21  0:54                 ` Bart Schaefer
2022-10-21  1:58                   ` Ray Andrews
2022-10-21  2:25                     ` Bart Schaefer
2022-10-21 14:24                       ` Ray Andrews
2022-10-21 14:37                         ` Ray Andrews
2022-10-21 17:34                         ` Roman Perepelitsa
2022-11-01  5:00                       ` "typeset -p" inconsistency Bart Schaefer
2022-11-01 12:07                         ` Peter Stephenson
2022-11-01 12:40                         ` Ray Andrews
2022-11-01 19:08                           ` Bart Schaefer [this message]
2022-11-01 21:25                             ` Ray Andrews
2022-11-01 21:40                               ` Bart Schaefer
2022-11-01 22:46                                 ` Ray Andrews
2022-11-02  1:13                                   ` Lawrence Velázquez
2022-11-02  2:42                                     ` Ray Andrews
2022-11-02  3:11                                       ` Lawrence Velázquez
2022-11-02 12:56                                         ` Ray Andrews
2022-11-02 17:04                                           ` Bart Schaefer
2022-11-02 17:19                                             ` Ray Andrews
2022-11-02 18:21                                               ` Bart Schaefer
2022-11-02  3:10                                   ` Bart Schaefer
2022-11-02 17:09                                     ` Ray Andrews
2022-10-20 17:29 ` var=$( typeset "$1" ) ... not within a function Mikael Magnusson
2022-10-20 17:43   ` Ray Andrews
2022-10-21 17:33 ` Ray Andrews
2022-10-21 18:25   ` Bart Schaefer
2022-10-21 18:57     ` Ray Andrews
2022-10-21 19:02       ` Roman Perepelitsa
2022-10-21 19:06         ` Ray Andrews
2022-10-21 19:04     ` Ray Andrews

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=7ZqryPVQGXkzPbr_DxkTpEJnGE19CL6iCo3-MEeBui=ug@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@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).