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

Re: Bug with unset variables



On Sat, Nov 14, 2020 at 1:52 AM Felipe Contreras
<felipe.contreras@xxxxxxxxx> 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.




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