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

Re: Bug with unset variables



On Thu, Nov 12, 2020 at 10:08 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
>
> On Thu, Nov 12, 2020 at 1:11 PM Roman Perepelitsa
> <roman.perepelitsa@xxxxxxxxx> wrote:
>>
> > Note that these two snippets have different effect:
> >
> >   var foo
> >
> > and
> >
> >   var foo
> >   delete foo
> >
> > Just line in zsh, and unlike ksh/bash.
>
> No. That code doesn't even work in JavaScript:

I didn't realize you cannot unset variables in JavaScript. Then
comparing it with shells isn't very useful.

>  "unset foo" and "foo = undefined" do *exactly* the same thing

`undefined` is just a value, so `foo = undefined` simply changes the
value of foo. You can still pass foo around, just like you could do it
if it held any other value. Unsetting a parameter in a shell is quite
different. It's impossible to detect a difference between these two
cases: 1) foo was never declared, and 2) foo was declared and
subsequently unset. (At least in global scope. Shells differ w.r.t.
local variables.)

We'll need another language that allows unsetting variables to have a
meaningful comparison. How about elisp? As I mentioned earlier, it
shares two important properties with ksh/bash/zsh -- dynamic typing
and dynamic scope. In elisp declaring a variable and then unsetting it
is not equivalent to just declaring a variable. Like in zsh and unlike
ksh/bash.

> In JavaScript "var foo" does not set foo to an empty string.

It sets *some* value, specifically `undefined`. In shells there is no
such value, so assigning `undefined` is not an option. The only
options are: assign the default value that corresponds to the
parameter's storage type (an empty string for string parameters) or
leave the parameter unset. The latter has no equivalence in JavaScript
because JavaScript has no notion of unset variables.

I don't think this discussion will affect anything of substance. I'm
continuing merely out of politeness. You've asked why I consider the
behavior of zsh natural and I'm doing my best to explain. I can see
that you consider the behavior of ksh/bash natural and I agree that
your position is consistent. I'm not arguing that what zsh does is
objectively *more* natural, only that it's also consistent and has
precedence in other languages (elisp).

I think I've found a language that has constructs equivalent to
typeset and unset with the same semantics as in ksh/bash. In Lua this
snippet:

  local x
  x = nil

Is equivalent to this:

  local x

Moreover, 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.

Roman.




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