zsh-workers
 help / color / mirror / code / Atom feed
From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: Felipe Contreras <felipe.contreras@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug with unset variables
Date: Sat, 14 Nov 2020 06:41:08 +0100	[thread overview]
Message-ID: <CAN=4vMqr-MBuzikqkSJEnY5=hW6xjv_TcW_paZV9oKEXnDsmrw@mail.gmail.com> (raw)
In-Reply-To: <CAMP44s15VJnjUmkXg6t2UgbCwN4M37Mfg7_URgvyAv6aVNs1wA@mail.gmail.com>

On Sat, Nov 14, 2020 at 1:52 AM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> In JavaScript if you don't declare foo, accessing it gives you the
> value of "undefined", and if you declare it and "unset" it, it also
> gives you "undefined".

Here's what I'm getting in Chrome console:

  console.log(foo)

    Uncaught ReferenceError: foo is not defined

  foo = undefined
  console.log(foo)

    undefined

Is this behavior non-standard?

> You just don't want to accept they are functionally the same because
> you don't want them to be the same.

Let's keep the discussion limited to the subject matter of programming
languages.

> Lisp doesn't allow defining variables without a value.

Here's what I'm getting in GNU Emacs 26.3:

  (defun foo ()
    (let ((var))
      var))

  (defun bar ()
    (let ((var))
      (makunbound 'var)
      var))

  (foo)
  nil

  (bar)
  *** Eval error ***  Symbol’s value as variable is void: var

A variable declared without a value gets the value of nil. You can
pass nil around like any other value.

> you have avoided some of my strongest arguments, for example this:
>
>   func () {
>     [[ -n "$1" ]] && var=$1
>     dosomething ${var-other}
>   }
>
>   func () {
>     typeset var
>     [[ -n "$1" ]] && var=$1
>     dosomething ${var-other}
>   }
>
> You have never explained how it makes sense that adding that extra
> line changes the behavior.

I thought you were arguing that the behavior of `typeset var` in
ksh/bash makes sense while in zsh it doesn't. However, in the example
you've given above adding `typeset var` to func changes the function's
behavior in all shells. What am I missing?

> > Moreover, [in Lua] variables to which nil has been assigned are
> > indistinguishable from variables that have never been declared.
> > "Variable foo is nil" has the same meaning as "variable foo does not
> > exist". Like in shells and unlike JavaScript.
>
> Exactly the same thing as in JavaScript, just s/nil/undefined/.

If you try to print a variable that hasn't been defined, you'll get an
error in JavaScript and "nil" in Lua.

> Either way, "local x" in Lua does exactly the same thing as it does in Bash.

That was my point. Originally I said I didn't know of any language
that does what ksh/bash does but then I realized that Lua could fit
the bill, so I mentioned it. To be more specific, I was looking for a
language that 1) allows you to declare variables without specifying
their values; 2) allows you to unset/unbind/undeclare variables; 3)
the effect of declaring a variable and immediately unsetting it is
equivalent to declaring a variable without specifying the initial
value.

FWIW, one of my gripes with Lua is that accessing an undeclared
variable gives you nil. If it was an error instead (which I would
prefer), Lua wouldn't satisfy the 3rd requirement I've listed above,
so it wouldn't be like ksh/bash for the purpose of this discussion. It
would be like zsh and elisp.

> The most straightforward way is not necessarily the best way.
>
> Very often the best way takes effort.

This is obviously true.

I'll summarize my position. I believe it is in agreement with Peter
and Bart but I wouldn't presume speaking for them.

- The way typeset works in zsh makes sense. There is no inconsistency,
there is precedence in other languages, and it was a natural evolution
from global-only parameters.
- If we could change history, it would be better if typeset in zsh
worked the same way as in ksh/bash because that behavior also makes
sense and compatibility is valuable.
- It's infeasible to change the behavior of typeset in zsh in native
mode because it'll break too much user code.
- It's feasible and desirable to make typeset compatible with ksh/bash
when KSH_TYPESET is set.

Roman.


  reply	other threads:[~2020-11-14  5:41 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
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 [this message]
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='CAN=4vMqr-MBuzikqkSJEnY5=hW6xjv_TcW_paZV9oKEXnDsmrw@mail.gmail.com' \
    --to=roman.perepelitsa@gmail.com \
    --cc=felipe.contreras@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).