[Bese-devel] Re: ucw:a & action parameters other than self

Marco Baringer mb at bese.it
Mon Apr 17 10:43:17 UTC 2006


Evrim ULU <evrim at core.gen.tr> writes:

> Hi,
>
> i'm trying to build a simple pager. When scroll-to-page action is
> called, to-page parameter is always "number-of-pages", not the current
> value of the iterator since loop increments the value of i. how can i
> avoid this problem? i've tried :action `(scroll-to-page ,self ,i) but
> didn't help :(
>
> --- inside render ---
> (loop
>      for i from 1 to number-of-pages
>      do
>      (if (eq current-page i)
>          (<:as-html i " ")
>          (<ucw:a :action (scroll-to-page self i)
>              :id (format nil "page-~A" i)
>              (<:as-html i " "))))
> ---- render ends ---

the problem is (scroll-to-page self i). ucw:a creates a closure around
the action parameter, but loop creates exactly one binding and
modifies that during the iteration. for example:

(loop
  for i from 1 to number-of-pages
  collect (lambda () i))

this code will create number-of-pages' closure but calling them will
always return the same value since they close over the same binding
and that binding is modified. however this:

(loop
  for i from 1 to number-of-pages
  collect (let ((another-i i))
            (lambda () another-i)))

will create closures which reference a freshly created binding whose
inital value is the value i had at the time the closure was created
and therefore will return different values. (try out the code for
yourself if this doesn't make sense).

to make along story short what you want to use is this:

(loop
   for i from 1 to number-of-pages
   do (if (eq current-page i)
          (<:as-html i " ")
          (let ((i i))
            (<ucw:a :action (scroll-to-page self i)
                    :id (format nil "page-~A" i)
                    (<:as-html i " ")))))

notice the let form which creates a new binding on each iteration of
the loop.

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen




More information about the bese-devel mailing list