zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: [BUG] Can't mark unset variables as read-only
Date: Wed, 29 Apr 2015 20:09:09 +0100	[thread overview]
Message-ID: <20150429190909.GB5530@chaz.gmail.com> (raw)
In-Reply-To: <5540F9B6.5020805@case.edu>

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.

-- 
Stephane


  reply	other threads:[~2015-04-29 19:27 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-29  6:35 Martijn Dekker
2015-04-29 10:36 ` Peter Stephenson
2015-04-29 10:57   ` Mikael Magnusson
2015-04-29 11:01     ` Mikael Magnusson
2015-04-30  0:08       ` PATCH: Don't define internal params directly in hook function scope Mikael Magnusson
2015-04-30  4:01         ` Bart Schaefer
2015-04-30  8:44           ` Peter Stephenson
2015-04-30 11:18             ` Peter Stephenson
2015-04-29 11:09     ` Local readonly specials (was: Can't mark unset variables as read-only) Peter Stephenson
2015-04-29 13:46   ` [BUG] Can't mark unset variables as read-only Martijn Dekker
2015-04-29 13:55   ` Bart Schaefer
2015-04-29 14:41     ` Peter Stephenson
2015-04-29 15:33       ` Chet Ramey
2015-04-29 19:09         ` Stephane Chazelas [this message]
2015-04-29 23:22           ` Chet Ramey
2015-04-30  3:57         ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2015-04-29  5:48 Martijn Dekker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150429190909.GB5530@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).