[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
- Subject: scoping...
- From: kab (Kim Barrett)
- Date: Tue, 29 Jun 93 19:41:39
> (apply #'add-menu-items foobar
> (let ((activities #(A B C))
> (items nil)
> ;(declare (<WHAT GOES HERE?!!!> activity)
> (dotimes (i (length activities) (nreverse items))
> (setq activity (aref activities i))
> (make-instance 'menu-item
> :menu-item-title (string activity)
> #'(lambda ()
> (print activity)))
> This results in a list of menu-items, each with the correct title, but the
> functions all affect the LAST activity, rather than the ith. ie, All of
> the menu-items print C.
> I understand that the variable activity is incorrectly scoped, but what do
> I do to fix it?
Move the binding of activity inside the loop, i.e.
(apply #'add-menu-items foobar
(let ((activities #(A B C))
(dotimes (i (length activities) (nreverse items))
(let ((activity (aref activities i)))
;(declare (<type of entries in activities> activity))
:menu-item-title (string activity)
This way, each of the menu item actions is closing over a distinct binding,
rather than having them all closing over the same binding that got setq'ed