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

Re: More rabbit-holes with unset variables



On Thu, Nov 26, 2020 at 6:23 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> On Thu, Nov 26, 2020 at 3:53 PM Felipe Contreras
> <felipe.contreras@xxxxxxxxx> wrote:
> >
> > > There's no way to "see" the export namespace without forking an
> > > external process, so only the internal value matters.
> >
> > No. The exported value exists whether you decide to look at it or not.
>
> The point is that the exported value DOES NOT exist in this example;
> if you were to look at the C global "environ" array following "export
> FOO", it would not have (a pointer to a string containing) "FOO" in
> it.

How do you know?

The example brought by Oliver was this:

  export FOO
  echo ${FOO-replacement}

How do you know FOO doesn't have a value beforehand?

That's precisely the reason the "-replacement" part is there: FOO may
or may not have a value.

You used precisely this argument when I brought up this example:

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

You said: "It's not possible to write deterministic code."

It is exactly the same thing here.

If we have this example:

  [[ -n "${1+set}" ]] && FOO=$1
  func () {
    export FOO
    sh -c 'echo "external: \"${FOO-nil}\""'
    echo "internal: \"${FOO-nil}\""
  }
  func

  % sh example.sh bar
  external: "bar"
  internal: "bar"

All shells I have return the same.

But when I do the same without argument:

  % sh example.sh
  external: "nil"
  internal: "nil"

In all shells, except:

  % zsh example.sh
  external: "nil"
  internal: ""

The inconsistency between the internal and external value *only*
happens in zsh, and it most definitely exists.

> > > >   typeset -x FOO
> > > >
> > > > Is different than this:
> > > >
> > > >   typeset -x FOO=""
> >
> > If this is not inconsistent, then nothing is.
>
> Now I'm confused.  All along you've been arguing that { typeset FOO }
> SHOULD differ from { typeset FOO="" }.  Why does adding -x invert your
> argument?

It doesn't.

To be consistent, either these two are the same:

  typeset -x FOO
  typeset -x FOO=""

Or these two are different:

  typeset FOO
  typeset FOO=""

As long as in zsh none of these are changed, zsh is objectively
inconsistent. It is *two* inconsistencies.

What I think is obvious should be changed is the latter, so that:

  typeset -x FOO
  typeset FOO

In both cases FOO is "unset", both locally and externally.

And.

  typeset -x FOO=""
  typeset FOO=""

In both cases FOO would have an empty string both locally and externally.

I don't see how this *second* inconsistency isn't obvious.

Cheers.

-- 
Felipe Contreras




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