[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Requesting an alternate nameref feature

From: Dan Douglas
Subject: Requesting an alternate nameref feature
Date: Wed, 12 Dec 2012 12:04:41 -0600
User-agent: KMail/4.8.3 (Linux/3.4.6-pf+; KDE/4.8.3; x86_64; ; )

Hello. Could we possibly modify or create an additional variant of "typeset -n"
which produces "real" references rather than just dynamic references by name?
In other words, I'd like to be able to create reference variables that always
point to the instance of a variable that was visible at the time the reference
was created, similar to the way ksh93's nameref works.

While the current nameref implementation is tremendously valuable in writing
functions that manipulate non-local arrays, it does very little else that
couldn't already be done with Bash's indirect parameter expansion, or to solve
the encapsulation problem.

 $ bash+ -c 'function f { typeset -n y=$1; typeset x=bar; echo "$y"; }; x=foo; 
f x'

 $ mksh -c 'function f { typeset -n y=$1; typeset x=bar; echo "$y"; }; x=foo; f 

 $ ksh -c 'function f { typeset -n y=$1; typeset x=bar; echo "$y"; }; x=foo; f 
I can't think of a reason this couldn't coexist with dynamic scope in
principle, with some modification. For instance, Bash won't require a check
that forces variable names to be passed through the positional parameters as in

This feature would have similarities to "declare -g" in its ability to
tunnel around overloaded variable names in outer scopes, except would allow
both reading and writing to any scope from any deeper scope (provided the
reference itself hasn't been covered up). This would be extremely useful for
shell libraries.

Dan Douglas

reply via email to

[Prev in Thread] Current Thread [Next in Thread]