Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
More about empty-string named references
- X-seq: zsh-workers 53750
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: More about empty-string named references
- Date: Sun, 8 Jun 2025 22:18:13 -0700
- Archived-at: <https://zsh.org/workers/53750>
- List-id: <zsh-workers.zsh.org>
The "BUG:" thread is getting a bit long and hard to follow.
See https://www.zsh.org/mla/zsh-workers/53747
On Sun, Jun 8, 2025 at 3:21 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> The Zsh code "typeset -i var; var=1; var=2" can be loosely translated
> to the C code "int var; var=1; var=2". There is no fundamental difference
> between the initialization and subsequent assignments; both translate to
> the same C operation. Things are very different for the Zsh code
> "typeset -n ref; ref=var; ref=42", which can be loosely translated to the C
> code "int *ref; ref=&var; *ref=42". In this case the initialization and
> subsequent assignments translate to very different operations.
s/Zsh/Ksh/. Perhaps the Korn developers should have created a new
assignment operator or even a new substitution syntax to make namerefs
distinct from other parameters at all points where they are used. Not
a debate I'm likely to get deeply involved with. That implementation
is the "standard" that was chosen. Anyway, comparisons of shell with
C don't move me a lot.
> it's already the case that "typeset -a arr=" is not accepted. And rightly so!
> An empty string is obviously neither an empty array, nor an array that
> contains just the empty string.
This is true but an array ...
- can be discarded and replaced with any scalar by simple assignment.
- can be discarded and replaced by any other type by a new typeset
- cannot be converted from/to a scalar by typeset -a/+a
None of those hold for a nameref.
There is one thing about placeholders that I'm reconsidering, although
I haven't checked if it would be difficult to implement. In ksh:
$ typeset -n ref
$ echo $ref
ksh: ref: no reference name
One reason the zsh implementation doesn't do this is because:
% echo x ${} x
x x
That is, the empty parameter name is syntactically accepted by zsh in
many contexts, and behaves just like any unset parameter.
However, I'm open to discussion of a "no reference name" error on $placeholder.
> while ( ... ); do {
> var=0; # Reset "var" to it's default value
> ...
> }
>
> You can do the same for strings, floats, and arrays. You just have to
> use the appropriate default value. However, for references, that is
> currently not possible.
I don't see how it would ever be possible? Once a reference is
initialized, all assignments etc. apply to the referent. We'd need a
psuedo-value that can't ever be assigned to anything else, or an
assignment operator that prevents following the reference. Neither of
those strikes me as very workable. (This may very well be why the
"for ref ..." variant was invented.)
> It is detrimental because it implies that a statement like "typeset -nu ref=$1"
> will not necessarily initialize the reference. Innumerable users will have to
> figure out why their script exhibits a completely weird behavior
Would "no reference name" on dereference mitigate this?
Messages sorted by:
Reverse Date,
Date,
Thread,
Author