Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [BUG] Can't mark unset variables as read-only

2015-04-29 11:33:10 -0400, Chet Ramey:
> func()
> {
> 	local foo=bar
> 	echo inside: $foo
> }
> func
> echo outside: $foo
> Bash gives the following output:
> ./x26: line 5: local: foo: readonly variable
> inside: immutable
> outside: immutable
> If the purpose of readonly is to make a particular name/value pair
> immutable, I think that allowing a local variable to shadow it,
> especially if you're going to export that local variable, is a bad thing.

I don't agree.

The whole point of having local variables is to avoid problems
with clashing namespaces.

You can have:

f() {
  local foo=1 # f's own variable
  g xxx
  echo "$foo"

g() {
  local foo=2
  blah blah

(f and g possibly in different libraries)

By declaring foo local, you make sure that you're not clashing
with someone else's foo. 

You don't have to worry on how you name your functions like you
do in POSIX sh (where you'd need to call one variable f_foo, and
the other g_foo for intance).

Above, if you do:

f() {
  readonly foo=2
  g xxx
  echo "$foo"

You're basically breaking g for no good reason. f's doesn't want
the value of f to be modified, but g would not have modified it
since it's declaring its *own* foo variable.

Having said that, I don't remember ever using "readonly" in a
script so I can't say I care much.


Messages sorted by: Reverse Date, Date, Thread, Author