Subject: Re: unset "hash[key]" isn't matched with what "key" may be
From: Bart Schaefer <[email-masked]>
To: Zsh hackers list <[email-masked]>
Date: Tue, 9 Feb 2016 07:50:50 -0800
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Feb 9,  8:53am, Peter Stephenson wrote:
} Subject: Re: unset "hash[key]" isn't matched with what "key" may be
}
} All that occurred to me was an additional option to provoke an extra
} expansion step on the argument after locating the brackets.
} 
}   unset -? 'hash[$i]'
} 
} would then be reliable.  If the extra backslash stripping occurred early
} enough it wouldn't get in the way.

So what magic makes this work for typeset?

torch% typeset -A foo
torch% x='['
torch% typeset "foo[$x]"     
zsh: not an identifier: foo[[]
torch% typeset 'foo[$x]'		<-- note here
torch% typeset -p foo
typeset -A foo=( '[' '' )
torch% 

Or perhaps an equally interesting question is, is that behavior in fact
incorrect and typeset shouldn't do it either?

Ksh:

$ x='['
$ typeset -A foo
$ typeset "foo[$x]"
ksh: typeset: foo[[]: invalid variable name
$ typeset foo[$x]				<-- not an error?
$ typeset -p foo
typeset -A foo=(['[]']=)			<-- but weird
$ typeset 'foo[$x]'
$ typeset -p foo
typeset -A foo=(['$x']= ['[]']=)		<-- no expansion
$