[Top][All Lists]

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

Re: Local variables overriding global constants

From: Mike Frysinger
Subject: Re: Local variables overriding global constants
Date: Thu, 4 Apr 2013 12:45:16 -0400
User-agent: KMail/1.13.7 (Linux/3.8.3; KDE/4.6.5; x86_64; ; )

On Thursday 04 April 2013 10:20:50 Chet Ramey wrote:
> On 4/4/13 12:34 AM, Mike Frysinger wrote:
> >>> would it be possible to enable a mode where you had to explicitly
> >>> `declare +r` the var ?  being able to "simply" do `local FOO` allows
> >>> accidental overriding in sub funcs where the writer might not have even
> >>> realized they were clobbering something possibly important.
> >> 
> >> It's an idea, but I don't really like the idea of making declare +r,
> >> which is disallowed everywhere else, do something in just this one
> >> special context.  Maybe another flag.  I'll have to think on it.
> > 
> > is there a reason for not just allowing `declare +r` everywhere ?  seems
> > like the proposal fits nicely into the existing system (although you've
> > said you're not terribly happy with said system): you can do `declare
> > -gr` to get perm- read only before, or you can do `declare -r` to get
> > read only by default while still allowing sub functions to override if
> > they really want.
> The idea, I believe, behind `readonly' is that you want constants.  There's
> no reason to even offer the functionality if you can easily make things
> non-constant.

so would the more palpable thing be to introduce a new flag then like -R ?  
it'd be like -r, but you could override it in local scope by doing `declare 
+R`.  e.g. something like:
        declare -R VAR=val
        foo() {
                declare +R VAR=myval
                echo $VAR
        echo $VAR
        echo $VAR
this would show:
but doing something like `local VAR` or `declare VAR` in foo() would result in 
an error -- you need the explicit +R.

the issue i'm trying to solve is to provide an environment that protects the 
dev from accidental clobbers without preventing the small edge cases when the 
dev really truly wants to override something.  my proposal above satisfies 
that, but i'm not tied to specific details as long as i can support the stated 
use case.

Attachment: signature.asc
Description: This is a digitally signed message part.

reply via email to

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