From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8452 invoked by alias); 10 Nov 2010 00:19:32 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28399 Received: (qmail 6032 invoked from network); 10 Nov 2010 00:19:30 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS autolearn=ham version=3.3.1 Received-SPF: pass (ns2.melb.primenet.com.au: SPF record at spf-1.redhat.com designates 209.132.183.28 as permitted sender) Message-ID: <4CD9C64E.8060307@redhat.com> Date: Tue, 09 Nov 2010 15:08:14 -0700 From: Eric Blake Organization: Red Hat User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101027 Fedora/3.1.6-1.fc14 Mnenhy/0.8.3 Thunderbird/3.1.6 MIME-Version: 1.0 To: zsh-workers@zsh.org Subject: static vs. dynamic scoping X-Enigmail-Version: 1.1.2 OpenPGP: url=http://people.redhat.com/eblake/eblake.gpg Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------enigDD4A27942062AA93CC6D501E" X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 --------------enigDD4A27942062AA93CC6D501E Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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=3Dshow_archive.t= pl&source=3DL&listname=3Daustin-group-l&id=3D14951 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=3Dlocal; f2; echo $a; }; function f2 { echo $a; a=3Dchanged; }; a=3Dglobal; f1; echo $a' global local changed $ zsh -c 'function f1 { typeset a=3Dlocal; f2; echo $a; }; function f2 { echo $a; a=3Dchanged; }; a=3Dglobal; 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. --=20 Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org --------------enigDD4A27942062AA93CC6D501E Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/ iQEcBAEBCAAGBQJM2cZOAAoJEKeha0olJ0NqLhkIAIfXO3poPtNbOSTTZVBqg2Xw EFog5iWxfNEzGzVnWuJJ8khROlyRJOB0tobuSPavS+Ol3o9We3E7hwzcUTWGnuoj GrJKqbWTayYIzm2d2dgMeCTF94XwnCg3G39hL2h5bR6mGEL0Y3E/XYaUoGHIJxaK dWOvuptwYCQmPM8/K/iOBLrfqiBZhEA+LXVhNy90HhFnkvSdWfW9YBieNmEntyAl Oi7wCHfxZzMbWTd5qh2eGNmZ0W/ri+510zsK0Hc5RfViCuu0kK0REydfi88aPTIo 74GPCVzuVReJxzng3X1QMNjvTsFTWj0nQWLo/18fHXCBHtUmMFpt+XpPMc37TAA= =LsOq -----END PGP SIGNATURE----- --------------enigDD4A27942062AA93CC6D501E--