Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11735

Re: language inconsistency(wart) & RFE

From Linda Walsh <bash@tlinx.org>
Newsgroups gnu.bash.bug
Subject Re: language inconsistency(wart) & RFE
Date 2015-10-21 15:53 -0700
Message-ID <mailman.749.1445468020.7904.bug-bash@gnu.org> (permalink)
References <5621A1DD.90205@tlinx.org> <CAAZkfoJzXL_Gc0onFJDnfmHujSTfi=pWte-gsw5k5n24mveKOw@mail.gmail.com> <56226F73.4040908@tlinx.org> <20151019124228.GT27325@eeg.ccf.org>

Show all headers | View raw



Greg Wooledge wrote:
--- please note locations of 'auto_local'
> # at beginning....
> shopt -s auto_local
> # Pick unbiased random number from 0 to N-1 ($1 = N)
> # Returns value in variable r.
> rand() {
>   shopt -u auto_local
>   local max=$((32768 / $1 * $1))
>   while (( (r=$RANDOM) >= max )); do :; done
>   r=$(( r % $1 ))
>   shopt -s auto_local
> }
> 
> 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.
----
	Under my proposal, it is a shopt option that can be turned
on or off.  When it is off, things behave as they do no.  When it is
on, vars w/o a specific declarator, or declarator+"-g switch" would
be default to be local. 


> (If you want to attack "language warts", start with the inability to
> return values from functions to their callers!)
---
	They can -- unfortunately, it is by printing to, usually,
to stdout, and reading it in the parent (I agree this is a rather
roundabout and inefficient way to send return vals... 

I've thought about that, but running into inconsistencies in the
dynamic variable propagation.  It's hard to define an
"alternate syntax" when the primary syntax doesn't consistently
yield the same answer.  So need to figure that out first.

Hopefully the toggling of 'auto_local' isn't hard to understand:
it's like adding a -i or -l flag to var declaration -- doesn't
affect current values in vars, but only new assignments.



Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: language inconsistency(wart) & RFE Linda Walsh <bash@tlinx.org> - 2015-10-21 15:53 -0700

csiph-web