zsh-workers
 help / color / mirror / code / Atom feed
From: Eric Blake <eblake@redhat.com>
To: zsh-workers@zsh.org
Subject: static vs. dynamic scoping
Date: Tue, 09 Nov 2010 15:08:14 -0700	[thread overview]
Message-ID: <4CD9C64E.8060307@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2383 bytes --]

On the Austin Group mailing list, David Korn (of ksh93 fame)
complained[1] that bash's 'local' uses dynamic scoping, but that ksh's
'typeset' uses static scoping, and argued that static scoping is saner
since it matches the behavior of declarative languages like C and Java
(dynamic scoping mainly matters in functional languages like lisp):

[1]
https://www.opengroup.org/sophocles/show_mail.tpl?CALLER=show_archive.tpl&source=L&listname=austin-group-l&id=14951

I'm trying to standardize the notion of local variables for the next
revision of POSIX, but before I can do so, I need some feedback on two
general aspects:

1. Implementation aspect:
  How hard would it be to add static scoping to zsh?
  Is it something that can be added in addition to dynamic scoping, via
the use of an option to select the non-default mode (for example, 'local
-d' to force dynamic, 'local -s' to force static, and 'local' to go with
default scoping)?
  If both scoping forms are supported, is it worth making the default
scoping dependent on posix compliance (for example, 'local' means
dynamic scoping for 'emulate zsh' but static scoping for 'emulate sh'),
or should it be the same default for both modes?

2. User aspect:
  Is anyone aware of a script that intentionally uses the full power of
dynamic scoping available through 'local' which would break if scoping
switched to static?

Here's a sample shell script that illustrates the difference between the
two scoping methods.

$ ksh -c 'function f1 { typeset a=local; f2; echo $a; };
  function f2 { echo $a; a=changed; };
  a=global; f1; echo $a'
global
local
changed

$ zsh -c 'function f1 { typeset a=local; f2; echo $a; };
  function f2 { echo $a; a=changed; };
  a=global; f1; echo $a'
local
changed
global

In static scoping, function f2 does not shadow a declaration of a, so
references to $a within f2 refer to the global variable.  The local
variable a of f1 can only be accessed within f1; the behavior of f2 is
the same no matter how it was reached.

In dynamic scoping, function f2 looks up its call stack for the closest
enclosing scope of a variable named a, and finds the local one declared
in f1.  Therefore, the behavior of f2 depends on how f2 is called.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

             reply	other threads:[~2010-11-10  0:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-09 22:08 Eric Blake [this message]
2010-11-10 11:10 ` Peter Stephenson
2010-11-10 16:50 ` Bart Schaefer
2010-11-10 17:28   ` Eric Blake
2010-11-10 17:43     ` Eric Blake
2011-05-29  6:38     ` Thorsten Glaser
2010-11-10 17:22 ` Bart Schaefer
2010-11-10 17:30   ` Eric Blake
2010-11-10 18:01     ` Bart Schaefer

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=4CD9C64E.8060307@redhat.com \
    --to=eblake@redhat.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).