From: =?ISO-8859-15?Q?Andr=E9_Thieme?=
Subject: Accessing dynamic vars
Date: 
Message-ID: <d2m16g$ah5$1@ulric.tng.de>
Let's say we have this code:

(defparameter *bar* 10)

(let ((*bar* 15))
   (format t "bar: ~d~%" *bar*)
   (let ((*bar* 20))
     (format t "bar: ~d" *bar*)))


The second let is shadowing the first one which shadowed the global 
accessible value. Is it possible to access within the second let form 
the value of *bar* of the first let form? (or even the global value)
If so, how?


Andr�
--

From: Pascal Costanza
Subject: Re: Accessing dynamic vars
Date: 
Message-ID: <3b7ggtF6bka13U1@individual.net>
Andr� Thieme wrote:
> Let's say we have this code:
> 
> (defparameter *bar* 10)
> 
> (let ((*bar* 15))
>   (format t "bar: ~d~%" *bar*)
>   (let ((*bar* 20))
>     (format t "bar: ~d" *bar*)))
> 
> 
> The second let is shadowing the first one which shadowed the global 
> accessible value. Is it possible to access within the second let form 
> the value of *bar* of the first let form? (or even the global value)
> If so, how?

(let ((*bar* 15))
   ...
   (let* ((org-bar *bar*)
          (*bar* 20))
     ...))

It's probably a good idea to build a macro for your own dynamic 
variables with that feature.


Pascal
From: Harald Hanche-Olsen
Subject: Re: Accessing dynamic vars
Date: 
Message-ID: <pco3bu9cp8e.fsf@shuttle.math.ntnu.no>
+ Andr� Thieme   <······························@justmail.de>:

| Let's say we have this code:
| 
| (defparameter *bar* 10)
| 
| (let ((*bar* 15))
|    (format t "bar: ~d~%" *bar*)
|    (let ((*bar* 20))
|      (format t "bar: ~d" *bar*)))
| 
| 
| The second let is shadowing the first one which shadowed the global
| accessible value. Is it possible to access within the second let form
| the value of *bar* of the first let form? (or even the global value)

Pascal Costanza, in his eagerness to provide a workaround, forgot to
give you the answer to your question, which is no, plain and simple.
Perhaps the easiest way to get the kind of functionality that you seem
to want is to make a stack instead.

(defparameter ((*bar* (list 15))))
(let ((*bar* (cons 15 *bar*)))
   (format t "bar: ~d~%" (car *bar*))
   (let ((*bar* (cons 20 *bar*)))
     (format t "bar: ~d" (car *bar*))))

Then, if desired, hide the implementation details with a bit of
macrology.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Pascal Costanza
Subject: Re: Accessing dynamic vars
Date: 
Message-ID: <3b7js9F6a95btU1@individual.net>
Harald Hanche-Olsen wrote:
> + Andr� Thieme   <······························@justmail.de>:
> 
> | Let's say we have this code:
> | 
> | (defparameter *bar* 10)
> | 
> | (let ((*bar* 15))
> |    (format t "bar: ~d~%" *bar*)
> |    (let ((*bar* 20))
> |      (format t "bar: ~d" *bar*)))
> | 
> | 
> | The second let is shadowing the first one which shadowed the global
> | accessible value. Is it possible to access within the second let form
> | the value of *bar* of the first let form? (or even the global value)
> 
> Pascal Costanza, in his eagerness to provide a workaround, forgot to
> give you the answer to your question, which is no, plain and simple.

...this depends on whether you want to take side effects into account or 
not. From the current thread, you cannot assign to the binding of 
dynamic variables of outer scopes, so my suggested workaround would only 
miss side effects from other threads, and such side effects are probably 
not a good idea anyway because of the likeliness of race conditions.

Of course, if you want to be able to assign to bindings of dynamic 
variables of outer scopes, my suggestion isn't good enough.



Pascal