Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] (?) typeset array[position=index]=value

On 5/31/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Mon, May 31, 2021 at 11:18 AM Bart Schaefer
> <schaefer@xxxxxxxxxxxxxxxx> wrote:
>> On Wed, May 5, 2021 at 4:45 AM Stephane Chazelas <stephane@xxxxxxxxxxxx>
>> wrote:
>> > [typeset] chokes on lvalue='array[n=1]' or
>> > lvalue='assoc[keywith=characters]'
>> Hmm, I wonder if that should be considered a bug.
> This copies (tweaked for context) the code from parse.c at line 2006
> or thereabouts.
> 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?

% 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 )

Relatedly, this also seems very inconsistent:
% typeset -A foo
% foo=(a b c d)
% bar=(a b c d)
% unset 'foo[a]'
% unset 'bar[2]'
% typeset -p foo bar
typeset -A foo=( [c]=d )
typeset -a bar=( a '' c d )

So for regular arrays, unset will just set the element to the empty
string, for assoc arrays it removes the key and the value.
For regular arrays, assigning () to the element unsets(?) the element,
and for assoc arrays it is an error.

Mikael Magnusson

Messages sorted by: Reverse Date, Date, Thread, Author