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

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

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: Reverse Date, Date, Thread, Author