Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [BUG] zsh/param/private scoping error
- X-seq: zsh-workers 49355
- From: Marlon Richert <marlon.richert@xxxxxxxxx>
- To: Mikael Magnusson <mikachu@xxxxxxxxx>
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [BUG] zsh/param/private scoping error
- Date: Wed, 1 Sep 2021 19:11:56 +0300
- Archived-at: <https://zsh.org/workers/49355>
- In-reply-to: <CAHYJk3QpW5S22BEbVm9KX05_orkfK8pAw2QeMprVys0gN=pZYg@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- References: <CAHLkEDtxe8wtOZ8dWC2tUdhQBpSZtKjGc7MrzJYBJ9oPdjP9Pw@mail.gmail.com> <CAH+w=7agEjSXRYBuKyVsCM+HUqCX4-y975wfKe7bDgH+OLs3vA@mail.gmail.com> <CAHYJk3QpW5S22BEbVm9KX05_orkfK8pAw2QeMprVys0gN=pZYg@mail.gmail.com>
On Wed, Sep 1, 2021 at 7:03 PM Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>
> On 9/1/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> > On Wed, Sep 1, 2021 at 4:51 AM Marlon Richert <marlon.richert@xxxxxxxxx>
> > wrote:
> >>
> >> % () { private tst; () { tst= } }
> >> (anon):1: tst: attempt to assign private in nested scope
> >> %
> >>
> >> This is not how one would expect private variables to behave.
> >
> > But it's how variable scoping behaves in the shell language. If I do
> >
> > % () { local tst; () { tst=foo } }
> >
> > Then it is the tst in the surrounding scope that gets altered, not the
> > global one. If the inner function is INTENDED to create a global, it
> > should be using "typeset -g" explicitly.
>
> This seems weird to me, since private is stated to be "used to create
> parameters whose scope is limited to the current function body, and
> not to other functions called by the current function." If the
> private parameter is not in scope, then any called functions should
> behave as if it is not in scope, ie automatically create a parameter
> scoped to whatever level is above the one that has the private.
> (technically speaking, this is probably hard).
Also note that when _reading_ variables, the `private` keyword already
behaves like this, that is, the private variable is out of scope for
the inner function:
% () { ; private tst=bar; () { print tst=$tst } }
tst=
%
% tst=foo; () { ; private tst=bar; () { print tst=$tst } }
tst=foo
%
> If private parameters
> are only compatible with code that runs without warnings under setopt
> WARN_CREATE_GLOBAL, then we should document this.
That would largely defeat the purpose of private variables, wouldn't it? :)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author