[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lexical and dynamic scoping (was Re: Redefining locals)
- From: David Given <dg@...>
- Date: Thu, 11 Nov 2004 17:11:53 +0000
On Thursday 11 November 2004 16:45, Mark Hamburg wrote:
> I was going to describe PostScript as doing dynamic scoping, but what it
> really does is look things up in the dictionary stack which could be
> emulated in Lua through proper manipulation of the global environment.
If anyone knows old BBC Basic, that had dynamic scoping. It looked like this:
10 i = 1: REM set global
15 PRINT i: REM says 1
30 PRINT i: REM says 2
60 DEF PROCchange
70 i = i + 1: REM change the currently visible copy of i
100 DEF PROCsubroutine
110 LOCAL i: REM save the old value of i onto the stack
120 i = 42
130 PRINT i: REM says 42
150 PRINT i: REM says 43
You could do some... dubious... things with this. Particularly when you could
use EVAL("") to evaluate arbitrary expressions, which would refer to the
current scope. LOCAL could be used anywhere, but the old value would only be
restored on an ENDPROC.
(You couldn't make an array local. You could make its contents local:
FOR i=0 TO 50: LOCAL array(i): NEXT
Personally, I think dynamic scoping is a nightmare. You can do some very
powerful things with it, but in general it's very nearly impossible to use
its features and produce maintainable code. Well-written BBC Basic programs
were written to look as if scopes were completely distinct.
+- David Given --McQ-+
| email@example.com | "The README of fate is a complex program indeed."
| (firstname.lastname@example.org) | --- Reboot
+- www.cowlark.com --+