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

Re: [PATCH 1/3]: Add named references

On Sun, Feb 12, 2023 at 1:02 AM Oliver Kiddle <opk@xxxxxxx> wrote:
>   % typeset -n ref=arr[1]whatever
>   % echo $ref
>   onewhatever

Aha.  OK, that's pretty easy to fix.

> As I've mentioned, we already have the situation where the caller
> needs nothing to make a private grabable by a builtin:
>   private -a var
>   compadd -O var one two three
> And writing wrappers to e.g. compadd is useful.
> But I don't think we need to address references to privates in other
> scopes at this point before the other patches are even applied. May be
> better to consider whether there are other advantages in moving private
> to the main shell first as that may ease any implementation.

I think it would in fact be necessary to move private to the main
shell in order to fix this.  The second-worst hack in private is the
overloading of some parameter flags, and the internals of e.g.
createparam() et al. need to be able to distinguish the cases of the
original flag meaning from its overloaded meaning.  Builtins have the
advantage of not passing through that code path.

> > > >   typeset -n ref
> > > >   for ref in 'hash[(e)'${(k)^hash[(R)?*]}']'; do ...
> I can't think of a sensible way. Was just that it could be useful.

What about adding ${(K)array) to return array[1] array[2] ... ?

for ref in ${(K)hash}; do ...

I took a quick look at the implementation of (k) and (v) and (K) looks
do-able with sufficient messing around in paramsubst().  Which I'm not
feeling like undertaking myself just now, though.

> Best long term solution might be to allow arrays of references. Bash
> does allow arrays of integers.

Bash also has sparse arrays, its implementation is quite different.

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