zsh-workers
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug with unset variables
Date: Thu, 12 Nov 2020 12:46:53 -0600	[thread overview]
Message-ID: <CAMP44s09or4ZtTiPW8yz8K+jpVqFKQVZtQnNUfA0zVda5zFH-g@mail.gmail.com> (raw)
In-Reply-To: <CAN=4vMpGEfp6t3BKogOz+WM-EROaU6MTuodaPYdAeqsQiTOQ5Q@mail.gmail.com>

On Thu, Nov 12, 2020 at 2:46 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
> On Wed, Nov 11, 2020 at 9:42 PM Felipe Contreras
> <felipe.contreras@gmail.com> wrote:

> > In all these it should be obvious what would be the sensible default, but this:
> >
> >   typeset var
>
> Both bash and zsh are consistent. Regardless of the presence or
> absence of a storage specifier, bash leaves the variable unset while
> zsh sets it to the "natural" value of the parameter's storage type. By
> natural I mean neutral w.r.t. +=. If `typeset -i var` was setting
> `var` to zero while `typeset var` was leaving it unset, that would be
> inconsistent. In my opinion this would be worse than the behavior of
> bash and zsh.

No. Zsh is not consistent. I did not type /typeset var=''/, I typed
/typeset var/.

> The fact that unset parameters are also called "null" in ksh/bash/zsh
> invites confusion when comparing them to languages that can have
> parameters with null *values*. Those null values are first-class
> citizens. You can pass them as arguments to functions, store them in
> arrays, etc. Shells don't have null *values*, they just have unset
> parameters.

This is distinction without a difference, like saying we are not lost,
we just don't know where we are. Conceptually it is the same thing,
you are just using a different word for it. It's wordplay.

An unset variable is for all intents and purposes a variable with a null value.

> Most languages (in fact, all languages I know) either don't have the
> notion of an unset variable with function scope, or automatically give
> all declared variables values. The closest equivalent to ksh/bash/zsh
> I'm aware of is elisp because it also has dynamic typing and dynamic
> scope. elisp has the same notion of an unset variable as ksh/bash/zsh
> (they are called void in elisp). You can declare local variables with
> `let` and unset them with `makunbound`. These behave like `typeset`
> and `unset` in zsh -- in order to create an unset variable with
> function scope, you need to declare it and then unset. Declaring the
> variable without value won't do.

So you don't know JavaScript (one of the most popular languages today)?

  > var v
  > typeof v
  'undefined'

Even in Python and Ruby the way you "declare" variables without a type
is by setting them to the equivalent of the null value. This has
*exactly* the same effect as "local x". Once again it's a distinction
without a difference.

In Swift you can declare a variable with the type "Any", and by
default it has the nil value.

Virtually all languages have a way of declaring a variable with a
local scope, and *all* of them (including shell) have an idiom to do
it without assigning an empty string (except zsh).

> In sum, what zsh does makes sense to me and feels natural and
> consistent with other languages I know.

Carrying luggage without wheels also felt natural.

Humans can get used to anything.

You cannot tell me that if I originally have this:

  func () {
    [[ -n "$1" ]] && var=$1
    dosomething ${var-other}
  }

And I want to change the scope of the variable, so it's not set
globally (which can be done in plenty of languages), and then I do
this:

  func () {
    typeset var
    [[ -n "$1" ]] && var=$1
    dosomething ${var-other}
  }

It makes sense to *change* the behavior of the code.

Can you?

> That isn't to say that I
> consider the behavior of ksh/bash incorrect. It's a bit surprising but
> sensible. I could definitely get used to it. The strongest argument
> for changing zsh is consistency with ksh and bash. The strongest
> argument against it is that it'll break a lot of existing zsh code.
> It's not my call but to me this looks like a no-go.

This is a false dichotomy.

Adding a setopt option for the new behavior doesn't break a lot of
existing zsh code.

Cheers.

-- 
Felipe Contreras


  parent reply	other threads:[~2020-11-12 18:47 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 15:57 Felipe Contreras
2020-11-11 16:13 ` Roman Perepelitsa
2020-11-11 16:56   ` Felipe Contreras
2020-11-11 17:02     ` Roman Perepelitsa
2020-11-11 18:03       ` Felipe Contreras
2020-11-11 18:16         ` Roman Perepelitsa
2020-11-11 20:42           ` Felipe Contreras
2020-11-12  0:20             ` Mikael Magnusson
2020-11-12  1:10               ` Felipe Contreras
2020-11-12  8:45             ` Roman Perepelitsa
2020-11-12 10:47               ` Peter Stephenson
2020-11-12 18:48                 ` Bart Schaefer
2020-11-12 19:49                 ` Felipe Contreras
2020-11-12 18:46               ` Felipe Contreras [this message]
2020-11-12 19:10                 ` Roman Perepelitsa
2020-11-12 21:08                   ` Felipe Contreras
2020-11-13  8:51                     ` Roman Perepelitsa
2020-11-14  0:52                       ` Felipe Contreras
2020-11-14  5:41                         ` Roman Perepelitsa
2020-11-16 19:41                           ` Felipe Contreras
2020-11-16 20:22                             ` Roman Perepelitsa
2020-11-17 20:28                               ` Felipe Contreras
2020-11-18 22:45                                 ` Daniel Shahaf
2020-11-22  1:20                                   ` Felipe Contreras
2020-11-23  4:00                                     ` Daniel Shahaf
2020-11-23  6:18                                       ` Felipe Contreras
2020-11-19  2:59                                 ` Bart Schaefer
2020-11-22  1:50                                   ` Felipe Contreras
2020-11-17 20:54                             ` Bart Schaefer
2020-11-22  1:49                               ` Felipe Contreras
2020-11-23  6:48                                 ` Bart Schaefer
2020-11-23  7:26                                   ` Felipe Contreras
2020-11-23 20:26                                     ` Bart Schaefer
2020-11-23 23:39                                       ` Felipe Contreras
2020-11-24  0:52                                         ` Bart Schaefer
2020-11-25  8:46                                           ` Felipe Contreras
2020-11-27 15:44                                             ` Daniel Shahaf
2020-11-27 20:49                                               ` Felipe Contreras
2020-11-27 20:59                                                 ` Daniel Shahaf
2020-11-27 21:33                                                   ` Bart Schaefer
2020-11-27 23:37                                                     ` Daniel Shahaf
2020-11-27 23:45                                                       ` Bart Schaefer
2020-11-28  0:24                                                       ` Bart Schaefer
2020-11-28  7:32                                                         ` Bart Schaefer
2020-11-28 12:05                                                         ` Felipe Contreras
2020-11-12 19:26                 ` Bart Schaefer
2020-11-12 21:48                   ` Felipe Contreras
2020-11-13 22:17                     ` Bart Schaefer
2020-11-14  0:58                       ` Felipe Contreras
2020-11-11 18:36         ` Bart Schaefer
2020-11-11 21:08           ` Felipe Contreras
2020-11-11 17:02     ` Peter Stephenson
2020-11-11 18:05       ` Felipe Contreras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMP44s09or4ZtTiPW8yz8K+jpVqFKQVZtQnNUfA0zVda5zFH-g@mail.gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=roman.perepelitsa@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).