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

Re: Bug with unset variables



On Mon, Nov 23, 2020 at 3:39 PM Felipe Contreras
<felipe.contreras@xxxxxxxxx> wrote:
>
> My argument is about the consistency from user's perspective.

Consistent from what user's point of view?  One coming to zsh from
other languages or one long familiar with zsh?  Because zsh
development has consistently (ahem) sided with the latter.

> This is the example I gave to Roman, which went completely unresponded:
>
>   func () {
>     [[ -n "$1" ]] && var=$1
>     dosomething ${var-other}
>   }
>
>   func () {
>     typeset var
>     [[ -n "$1" ]] && var=$1
>     dosomething ${var-other}
>   }

In the first case, $var is a global, so the behavior of ${var-other}
is unknown.  It's not possible to write deterministic code.

In the second case, there's a knowable behavior of ${var-other}.  That
behavior doesn't match your expectation, but it's well-defined.

To make the first function deterministic, it is necessary to write:

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

Whether one should expect "typeset var" to imply "unset" is how we
ended up in this discussion.




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