Dynamic namespaces via exceptions
Alexandre Bergel
bergel at iam.unibe.ch
Sat May 14 10:31:24 UTC 2005
Hello Trygve,
You reproduced the mechanism of Lisp for resolving variable. This means that in Lisp, whenever you write the name of a variable, you do not know statically to which variable you are referring to. Whereas in Scheme, as in Smalltalk, you can statically determine to which variable a name is bound.
For instance, in Lisp you would write:
(defun a 5)
(defun f () a)
(let ((a 10))
(f)) ; ===> 10
(let ((a 20))
(f)) ; ===> 20
Whereas in Scheme, a similar code would yield:
(define a 5)
(define (f) a)
(let ((a 10))
(f)) ; ===> 5
(let ((a 20))
(f)) ; ===> 5
Cheers,
Alexandre
On Sat, May 14, 2005 at 11:41:45AM +0200, Trygve Reenskaug wrote:
> In BabyUML, I am experimenting with a very dynamic namespace that is put on
> the stack, that exists during the execution of a block, and that is visible
> to all methods called directly or indirectly from the block.
>
> It seems to work using the exeception mechanism, but I would appreciate
> assistance to check if I am doing something ugly or dangerous.
>
> The namespace class is called Map and is a subclass of Exception.
>
> I create aMap with the desired contents and put it on the stack with:
>
> [body statements] on: aMap do: [:ex | ].
>
> Any method called from the body statements get hold of the map by this
> method:
>
> map
> | ctx map |
> ctx := thisContext.
> [ ctx := ctx findNextHandlerContextStarting.
> ctx isNil or: [(map := ctx tempAt: 1) isKindOf: DNetMap]
> ] whileFalse: [ctx := ctx sender].
> ^ctx
> ifNil: [self error: 'No map found.'. nil]
> ifNotNil: [map]
>
> Many thanks for any tips or guidance.
> --Trygve
>
> (If this works, it illustratesthe extreme power of Squeak)
>
>
> --
>
> Trygve Reenskaug mailto: trygver at ifi.uio.no
> Morgedalsvn. 5A http://heim.ifi.uio.no/~trygver
> N-0378 Oslo Tel: (+47) 22 49 57 27
> Norway
>
>
--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel http://www.iam.unibe.ch/~bergel
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
More information about the Squeak-dev
mailing list
|