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

Re: [BUG] zsh/param/private scoping error



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