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

Re: Up-scope named references, vs. ksh



On Sun, Mar 3, 2024 at 5:44 AM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
>
> If that one can be fixed then why can't the main one

Because the outer-scope parameter object already exists in "that one".
As I keep saying, the problem is with creating a new parameter at a
lower scope (counting 0 as the global scope) when a parameter of that
name already exists at the current (greater than 0) scope.

>   typeset -n var=$1
>   typeset    value=$2

If the parameter is magically created "in advance" (at the point of
"typeset -n") then it only works for global scalars -- if you try to
create an array using "var" you run into baffling stuff like this:

assign:3: value: attempt to assign array value to non-array

I think it's much more consistent for dynamic scoping to apply at the
time $var is referenced or assigned.

The patch in workers/52650 allows something like this to work:

assign() {
  typeset value=$2
  typeset -nu var=$1 # ignore local $value
  case ${(t)var} in
  (*scalar*)  var=$value;;
  (*array*)  var=( $value );;
  (*) print -u2 cannot assign;;
  esac
}
declare -a value
assign value x

If a function wants to be able to actually add things to its calling
scope without fear of name clashes, it has to use unique local names
(e.g., a namespace).  But with dynamic scoping it remains the case
that you can't add a name to the global scope if any function in the
call chain has pre-empted that name -- named references don't change
this basic rule.




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