lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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
17 PROCchange
20 PROCsubroutine
30 PRINT i: REM says 2
40 END
50 :
60 DEF PROCchange
70 i = i + 1: REM change the currently visible copy of i
80 ENDPROC
90 :
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
140 PROCchange
150 PRINT i: REM says 43
160 ENDPROC

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

Aaaaaaah!)

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-+ 
|  dg@cowlark.com    | "The README of fate is a complex program indeed."
| (dg@tao-group.com) | --- Reboot
+- www.cowlark.com --+