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

Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff]

2021-06-02 07:52:25 -0700, Bart Schaefer:
> > I have to admit I don't see the problem here. I would have
> > thought allowing a[]=foo and unset 'a[]' would be no-brainers
> Mostly I was thinking about
> key=$(some derived value)
> unset "hash[$key]"
> In existing code that would fail on [[ -z $key ]], but you can't see
> that by examination.

Not sure I follow. That script doesn't work properly atm as it
fails to unset the corresponding hash element and would be fixed
once we allow unset 'hash[]'

I can't think of real life scenarios where one would *rely* on

unset 'hash[]'

Aborting the shell with a

zsh:unset:1: hash[]: invalid parameter name


> > as there's no concern about backward compatibility as those
> > currently return an error.
> That's not our usual criteria for backward compatibility.  Usually we
> only change things if the new construct was previously a syntax error,
> something that would prevent an old script from even being properly
> parsed.

By that logic, we could never add features like new options to
builtins or new flags. For instance, we couldn't add a -k option
to unset because

unset -k key arr

currently is not a zsh syntax error, that script is parsed OK,
and that command returns with:

unset: bad option: -k

> > Even for plain arrays, IMO, it would make sense to allow empty
> > subscripts. In most contexts, an empty arithmetic expression is
> > interpreted as 0:
> But ... there's no such thing as array position 0 in native zsh.

But it would make the API more consistent if array subscripts
could be any arithmetic expressions or comma-separated pair of
arithmetic expressions.

And when ksharrays is not enabled a[] to return the same error
as for a[0] or a[empty] ("assignment to invalid subscript

Note that 0 position is allowed in second place:

for instance (or a[2,empty]=x, but not a[2,]=x atm) to insert a
x in second position.

I'm not saying that's something we should do or would be
terribly useful, just that it would make the interface more
consistent, and that array[] being rejected should not be a
justification for rejecting assoc[].

> > The fact that (e) is recognised and (ee) is
> > not also makes for a not very consistent API.
> What would (ee) mean?

The e flag passed twice.

echo $a[(e)*], $a[(ee)*], $a[(eee)*]

All expand to element of key "*".


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