[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: language inconsistency(wart) & RFE
From: |
Greg Wooledge |
Subject: |
Re: language inconsistency(wart) & RFE |
Date: |
Mon, 19 Oct 2015 08:42:28 -0400 |
User-agent: |
Mutt/1.4.2.3i |
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!)
Re: language inconsistency(wart) & RFE, Chet Ramey, 2015/10/19