Zsh Mailing List Archive
Messages sorted by:
Re: [PATCH] (?) typeset array[position=index]=value
- X-seq: zsh-workers 49023
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH] (?) typeset array[position=index]=value
- Date: Sat, 5 Jun 2021 13:06:03 +0200
- Archived-at: <https://zsh.org/workers/49023>
- In-reply-to: <CAH+w=7aVdHRYCnpXdO6E+FwMoM5jvVvD+1am70k76ca8q5-WyQ@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <email@example.com> <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org> <email@example.com> <firstname.lastname@example.org> <email@example.com> <CAH+w=7bHxSbFr60ZU0+oZ6+qEejhfBYTzvL7=aXadY5XzWtSzw@mail.gmail.com> <firstname.lastname@example.org> <CAH+w=7ZqE2DnxpHhvCjZnXB4A1vJ=EKB2fpWyUMaZX0VYqU9kg@mail.gmail.com> <CAH+w=7bVn2LiTsq194GjshVSOCSib7t4T=uZQ2ZvUGu6Z2XoPw@mail.gmail.com> <CAHYJk3RSDVjxAPhz5Z+1D31-tZtL5qFonokPY=tTauK8RzAXew@mail.gmail.com> <CAH+w=7aVdHRYCnpXdO6E+FwMoM5jvVvD+1am70k76ca8q5-WyQ@mail.gmail.com>
On 6/5/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 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?
Well, there was 4 different threads about saying "unset foo[$bar]" and
I didn't know exactly where to reply so I went to the source, maybe I
went one thread too far back though.
>> % 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
> foo=( [a]=() )
> foo[a]=( z )
set foo[a] to z
> foo[a]=( x y )
there probably aren't more cases
>> 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).
By this logic, unset "foo[bar]" should have had the same effect as
foo[bar]="" (also too late).
>> and for assoc arrays it is an error.
> Because you can't perform a splice on a hash table.
Well, with my naive end user hat, this seems much more logical to
remove an element than fiddling with the unset keyword, which doesn't
even do the same on a normal array. If you come from knowing that
bar=() removes the 5th element, then it's reasonable to assume that
foo[baz]=() would work too.
Since we've established that both unset and assignment to an element
behaves in no way the same already for regular arrays and associative
arrays, is there any reason to not make assignment to associative
array slices (with the empty slice) just remove the element? The
parsing already works and it wouldn't break any existing cases.
Messages sorted by: