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

Re: Bug with unset variables



On Wed, Nov 11, 2020 at 12:17 PM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Wed, Nov 11, 2020 at 7:03 PM Felipe Contreras
> <felipe.contreras@xxxxxxxxx> wrote:
> >
> > And what is the reason why this is not the default?
>
> I don't know, I'm fairly new to zsh myself. All I can say is that the
> behavior of zsh feels more natural to me.

Why would

  typeset var

feel natural to be the same as

  typeset var=""

?

If I wanted the second, I would type the second.

Why does it feel natural to assign a value of a certain type, when no
value was specified?

> > Not only does this behavior differ from all other shells, but
> > basically all languages.
>
> All languages? That seems exaggerated. How about this?
>
>   int var;

You are specifying a type. I obviously meant all languages where you
can do the equivalent of "declare var" (without a type).

That being said, the most similar to shell's "declare var" in C is
"char *var" which defaults to null on most systems.

> How many languages will set `var` to null here? Is it all of them?

Pretty much, yeah. Do you want me to investigate and list them? Would
the result of that investigation change the view of zsh developers?

> In the morally-equivalent shell code, bash does but zsh doesn't:
>
>   typeset -i var
>   echo $var

In this case it might make sense to initialize to 0, since that's the
only sensible default for an integer, but consider these:

  typeset -i int
  typeset -a array
  typeset -A hash
  typeset -F float

In all these it should be obvious what would be the sensible default, but this:

  typeset var

Does not specify any type (or any value), so why would you assign a
value of a possibly wrong type?

It becomes more obvious when you print the declaration at each step:

  typeset var
  typeset -p var
  var=()
  typeset -p var

In bash, we get something sensible:

  declare -- var # no type specified
  declare -a var=()

Not so in zsh:

  typeset var='' # why a string?
  typeset -a var=(  )

Not only is it inconsistent superficially, but also internally, since
strings are considered "scalar", but that is just another word for
"variable". An integer is also a scalar. So the word "scalar" doesn't
really explain any type (only that it is a single value).

> I don't know which of these languages conforms to the majority opinion
> but neither can claim to do what *all* languages do.

It is the most sensible thing to do, so I bet at least 99.9% of
languages do this.

It is also good, desirable, and consistent, that this:

  typeset var
  typeset -p var

is a roundtrip, which happens in bash, but not so in zsh.

> FWIW, I rely on this behavior of zsh in my code (namely, that `typeset
> -i var` sets `var` to 0).

That may make sense, because you are specifying a type, but "typeset
var" doesn't specify a type. Apples and oranges.

Cheers.

-- 
Felipe Contreras




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