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

Re: named references



Andrej wrote:

> Apart from obvious "it was fun to implement" - can somebody give an example
> for use of nameref? I mean, real examples, where other means are
> impossible/have disatvantage over nameref. To be honest, I've tried to
> imagine one and failed (ksh93 compatibility issue aside).

Any situation where you want a function to return a value and the exit
code is not sufficient. This can currently be done with an eval but it
is messy, especially with associative arrays. The nameref can be used
like any normal variable so you end up with more readable code:

at the moment, you would need to define a local for the return value,
and before returning, assign it across with eval:
  typeset -A $1
  eval $1'=( ${(kv)ret} )'
it is much more readable if the function just starts with:
  nameref ret="$1"

If we ever implement other more complex parameter types (such as
something like ksh93 namespaces), it would be even more useful because
the eval code for them could be even more messy.

Also, unlike eval, namerefs can reference variables at a higher local
level so in the above `ret' could be passed as $1 safely. I'd also
expect that if we ever implement anything like dtksh/tksh as a module,
more scripts would be written which would use namerefs.

Namerefs could also be useful for simple cases where an alternate name
is wanted, maybe to allow a different language such as we have already
done with ZLS_COLOURS/ZLS_COLORS or for things like prompt. You even
suggested some sort of parameter aliasing in 12830.

Does anyone else have an opinion on whether namerefs would be a useful
addition? I'd prefer this to be decided now rather than after I spend
more time on them.

Oliver



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