Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #11704
| From | Greg Wooledge <wooledg@eeg.ccf.org> |
|---|---|
| Newsgroups | gnu.bash.bug |
| Subject | Re: language inconsistency(wart) & RFE |
| Date | 2015-10-19 08:42 -0400 |
| Message-ID | <mailman.632.1445258558.7904.bug-bash@gnu.org> (permalink) |
| References | <5621A1DD.90205@tlinx.org> <CAAZkfoJzXL_Gc0onFJDnfmHujSTfi=pWte-gsw5k5n24mveKOw@mail.gmail.com> <56226F73.4040908@tlinx.org> |
On Sat, Oct 17, 2015 at 08:55:31AM -0700, Linda Walsh wrote:
> If I needed a way to declare something global, yes...
> But what I am wanting is a way to allow changing the defaults
> of the implicit variable creation (which could still be
> explicitly declared with "-g" if one wanted their result to be
> made global.
So you are basically saying you want all of your function variables
to be local, but you are too lazy to write 'local i j k' and you want
bash to do it for you?
Also I think you are completely misrepresenting the dynamic variable
scope system that bash uses. Variables are not just global or local.
There's an entire stack of them. When you reference a variable (let's
say i) inside a function, bash searches up through the call stack
looking for a variable named i until it finds one.
Since functions cannot return values to their callers, the entire system
of "put values into an upper-scope variable so the caller can see them"
would break if your proposal of automatic localization were to be
adopted.
# Pick unbiased random number from 0 to N-1 ($1 = N)
# Returns value in variable r.
rand() {
local max=$((32768 / $1 * $1))
while (( (r=$RANDOM) >= max )); do :; done
r=$(( r % $1 ))
}
foo() {
local r
rand 6
echo "I rolled $((r+1))"
}
foo
# r is not visible here
Under your proposal, the variable r which is defined locally in foo, and
is up-scope-visible to rand (so that rand can put a return value into
it), would also be defined locally within r, so there would be no way to
return a value from rand to foo.
(If you want to attack "language warts", start with the inability to
return values from functions to their callers!)
Back to gnu.bash.bug | Previous | Next | Find similar
Re: language inconsistency(wart) & RFE Greg Wooledge <wooledg@eeg.ccf.org> - 2015-10-19 08:42 -0400
csiph-web