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

Re: More rabbit-holes with unset variables

On Thu, Nov 26, 2020 at 5:30 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
> What zsh refers to as "scalar" internally is a string:
>   char *str; /* value if declared string  (PM_SCALAR)  */
> From Src/zsh.h (struct param).

This is exactly the discussion I was trying to avoid when I said "in
the abstract that doesn't matter".

You can't just pull one field out of a union inside a struct and
ignore the struct itself and the API for field access that goes with

> So if you didn't mean string, what did you mean?

I meant a struct param, containing the least specific thing so
represented, as interpreted through all the layers of code that
implement a dereference of its value when you write $var or any of its
variations.  Again this doesn't actually matter, which is why I didn't
spell it out.

> And what did you mean by 'so a the only useful "declared but not set"
> variable is a simple scalar'?

As the very first message in this thread demonstrated, in both bash
and ksh (call this "example one", and to be pedantic assume that X is
not inheriting its name or value from somewhere):

typeset -i X
echo ${X-nil}
echo ${X-nil}

will output


However (call this "example two"):

typeset -i X
unset X
echo ${X-nil}



The language you quoted from the posix proposal says "otherwise, the
variable is initially unset".  Given that proposed language, example
one is incorrect, because an "unset" variable should not retain its
(in this example) integer properties when assigned a string.

> What simple scalar other than a string is useful "declared but not set"?

Under this interpretation, there isn't any.  That's what I said.  In
fact the last paragraph of the very first message in this thread:

"Therefore, this isn't as simple as having zsh create an unset
variable when typeset is given no assignment, because subsequent
assignment has to preserve the type of the variable, which normally
does not apply after unset."

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