zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: PATCH: [key]=value syntax, work in progress
Date: Wed, 13 Sep 2017 09:53:38 +0100	[thread overview]
Message-ID: <20170913095338.25a469f8@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <CAH+w=7bTW6Gjiq0mhnQUe9FAXaPjLD=NZqqRmZkiLS2CcxnLYQ@mail.gmail.com>

On Wed, 13 Sep 2017 00:13:35 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> >  If the parameter var(name) exists and is a scalar, it is replaced by a new
> >  array.  To append to an array without changing the existing values, use
> > -the syntax:
> > +one of the following:
> >  ifzman()
> >  indent(var(name)tt(+=LPAR())var(value) ...tt(RPAR()))
> > +indent(var(name)tt(+=LPAR())tt([)var(key)tt(]=)var(value) ...tt(RPAR()))
> 
> What does "append" mean when the keys are specified?  If I have
> 
> arr=( 1 2 3 4 5 )
> 
> and I do
> 
> arr+=( 6 [2]=7 8 )
> 
> does that even make sense?  That's certainly not "appending" to arr[2].

That's an override; it will work, but as you say describing it as
"append" isn't very helpful, so some extra words are useful.  There's
prior art for this as it's how associative arrays already behave:

% typeset -A hash=(one un two deux)
% hash+=(one eins)
% print -aC2 ${(kv)hash}
one  eins
two  deux

> The other point that this raises is that in ksh "typeset -p" of an associative
> array outputs e.g.
> 
> typeset -A x=([y]=2 [z]=1)
> 
> and that's the only way to assign an associative array; if you assign without
> the [k]=v syntax the parameter converts into an ordinary array.  Is this going
> to get enforced when KSH_ARRAYS and/or KSH_TYPESET are in effect?

That's probably a good idea.

By the way, the mixed syntax is going to be a nightmare:

$ array=(* [100]=foo)

is entirely valid, which means much more work either passing through
a list with extra structure to indicate the differences or trawling
through them to fix up one way or the other, in either case with
different expansion rules applied to different elements individually.

I suppose it's not that common, so doesn't have to be particularly
efficient as long as the traditional zsh way isn't affected, so I'm
inclined to the latter, i.e. convert to the new format if we detect it
in any element. The biggest problem there is that we don't know it's an
associative array till later so it's just going to have to be a place
marker that just gets removed in that case.  And then there are cases
like

typeset -A hash
hash=(key1 [key2]=value)

Plus I'd like to avoid an extra trawl through the entire list of
arguments just to check the format.

pws


  reply	other threads:[~2017-09-13  8:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-11 20:51 Peter Stephenson
2017-09-11 20:56 ` Peter Stephenson
2017-09-12 20:25 ` Peter Stephenson
2017-09-12 20:28   ` Peter Stephenson
2017-09-13  7:13   ` Bart Schaefer
2017-09-13  8:53     ` Peter Stephenson [this message]
2017-09-13 16:44       ` Bart Schaefer
2017-09-14 20:48         ` Peter Stephenson
2017-09-19  1:13           ` Bart Schaefer
2017-09-13  9:14   ` Oliver Kiddle
2017-09-13 19:41     ` Peter Stephenson
2017-09-14  7:03       ` Daniel Shahaf
2017-09-14 20:38       ` Peter Stephenson
2017-09-16 10:30         ` Daniel Shahaf
2017-09-14  6:54     ` 'typeset -p' of assocs (was: Re: PATCH: [key]=value syntax, work in progress) Daniel Shahaf

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=20170913095338.25a469f8@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@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).