Zsh Mailing List Archive
Messages sorted by:
Re: [PATCH] (?) typeset array[position=index]=value
- X-seq: zsh-workers 49021
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Mikael Magnusson <mikachu@xxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH] (?) typeset array[position=index]=value
- Date: Fri, 4 Jun 2021 22:49:14 -0700
- Archived-at: <https://zsh.org/workers/49021>
- In-reply-to: <CAHYJk3RSDVjxAPhz5Z+1D31-tZtL5qFonokPY=tTauK8RzAXew@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org> <CAH+w=7bHxSbFr60ZU0+oZ6+qEejhfBYTzvL7=aXadY5XzWtSzw@mail.gmail.com> <email@example.com> <CAH+w=7ZqE2DnxpHhvCjZnXB4A1vJ=EKB2fpWyUMaZX0VYqU9kg@mail.gmail.com> <CAH+w=7bVn2LiTsq194GjshVSOCSib7t4T=uZQ2ZvUGu6Z2XoPw@mail.gmail.com> <CAHYJk3RSDVjxAPhz5Z+1D31-tZtL5qFonokPY=tTauK8RzAXew@mail.gmail.com>
On Fri, Jun 4, 2021 at 9:29 PM Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> On 5/31/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > All tests still pass, but as you can see from the comment this is not
> > yet handling x+=y which there doesn't seem to be any reason for
> > typeset NOT to support; I think it would require only another flag in
> > struct asgment, but I haven't attempted that.
> > Commentary?
> Is there some fundamental reason we couldn't just make the obvious
> thing work instead?
For one thing, because the above isn't about what happens to the
value, it's about whether assignment can understand the key?
> % typeset -A foo
> % foo=(a b c d)
> % foo[a]=()
> # what actually happens
> zsh: foo: attempt to set slice of associative array
> # what seems reasonable to happen
> % typeset -p foo
> typeset -A foo=( [c]=d )
The problem is with the follow-up questions, namely ,what should happen with
foo=( [a]=() )
foo[a]=( z )
foo[a]=( x y )
> Relatedly, this also seems very inconsistent:
> So for regular arrays, unset will just set the element to the empty
> string, for assoc arrays it removes the key and the value.
That's because zsh doesn't support sparse arrays, and the NULL element
indicates the end of the array, so you can't put a NULL element in the
middle. If we'd thought about it long ago, it might be an error to
unset a regular array element, but we're stuck now.
> For regular arrays, assigning () to the element unsets(?) the element
No. For regular arrays assigning an array to an element splices the
rvalue array into the lvalue array. Splicing the empty array
shortens the regular array, it doesn't cause elements to become unset
(unless you consider that the former $#'th element is now unset
because that position no longer exists).
> and for assoc arrays it is an error.
Because you can't perform a splice on a hash table.
Messages sorted by: