help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: feature request, vars local only to function running, not sub functi


From: Koichi Murase
Subject: Re: feature request, vars local only to function running, not sub functions running
Date: Thu, 13 May 2021 12:14:33 +0900

2021年5月13日(木) 11:08 Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>:
> how to explain in other words..

It's called "lexical scoping" or "static scoping". I think it would be
very useful if we can easily implement the lexical scoping in addition
to the dynamic scoping, but I'm not sure if that can be easily done in
the Bash codebase. I found an old discussion:

https://lists.gnu.org/archive/html/bug-bash/2010-11/msg00036.html
09 Nov 2010 23:00, Chet Ramey:
> On 11/9/10 4:52 PM, Eric Blake wrote:
> > 1. Implementation aspect:
> >   How hard would it be to add static scoping to bash?
> >   Is it something that can be added in addition to dynamic scoping, via
> > the use of an option to select the non-default mode (for example, 'local
> > -d' to force dynamic, 'local -s' to force static, and 'local' to go with
> > default scoping)?
>
> The scoping is at the function level, not the variable level, so this
> would not be possible without more work than I'd be willing to do.

Also, I think we need to carefully design the interaction between the
dynamically-scoped variables and the statically-scoped variables. For
example, what does happen in the following case? Does it print
"dynamic" or "global"?

foo=global
func1() { local foo=dynamic; func1; }
func2() { local -L foo=lexical; func2; }
func3() { echo $foo; }

In addition, I think name references will also make things complicated
since they are not true references (i.e., pointers to the variable
placeholders) but just variable "names". In languages with lexical
scoping, we usually have by-value and by-reference argument passing to
circumvent the limitation of the lexical scoping, and maybe we would
want to add true references for by-reference argument passing if we
support lexical scoping.

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]