Problem with macro with-timer

Andrea De Michele andrea.demichele at gmail.com
Tue Mar 4 18:01:04 UTC 2014


Philippe Brochard <pbrochard at common-lisp.net> writes:

> Andrea De Michele writes:
>
>> I tried to call the macro with-timer like this:
>>
>> (with-timer (2) (focus-frame-by (find-frame-by-name "notification")))
>>
>> But sbcl give me the following error:
>>
>> The variable #:G1442 is unbound.
>>
>> If I call the macro with the optional parameter id like this all works:
>>
>> (with-timer (2 (gensym)) (focus-frame-by (find-frame-by-name "notification")))
>>
>> I think (but I'm not sure) that the problem is that the default value
>> for the optional parameter id is evaluated before macroexpansion.
>>
>> If I change the definition of with-timer from:
>>
>> (defmacro with-timer ((delay &optional (id (gensym))) &body body)
>>   "Same thing as add-timer but with syntaxic sugar"
>>   `(add-timer ,delay
>> 	      (lambda ()
>> 		, at body)
>> 	      ,id))
>>
>> to:
>>
>> (defmacro with-timer ((delay &optional (id '(gensym))) &body body)
>>   "Same thing as add-timer but with syntaxic sugar"
>>   `(add-timer ,delay
>> 	      (lambda ()
>> 		, at body)
>> 	      ,id))
>>
>> both the way to call the macro (with and without the optional id parameters) works.
>>
> Hi, thanks for the report. You're right, the gensym is created only
> once at compilation time. Your second version is the right thing to do
> since a gensym is created each time we call add-timer.
> The fix is applied in your name.
>
> Regards,
>
> Philippe
>

I think that gensym it is not created only once at compile time, but gensym as
default value is evaluated each time before macroexpansion. 

If I evaluate several time this form:

(with-timer (2) (focus-frame-by (find-frame-by-name "notification")))

the error is each time different:

CLFSWM> (with-timer (2) (focus-frame-by (find-frame-by-name "notification")))
; Evaluation aborted on #<UNBOUND-VARIABLE G1462 {1003A88ED3}>.
CLFSWM> (with-timer (2) (focus-frame-by (find-frame-by-name "notification")))
; Evaluation aborted on #<UNBOUND-VARIABLE G1463 {1003B1B463}>.

gensym is evaluated each time and its return value is each time
different (#:G1462 and #:G1463 in this case)

I have found an old blog post that explains this strange macro parameter
behaviour:

http://www.findinglisp.com/blog/2005/01/keyword-parameters-in-macro-expansions.html

PS: thank you very much for this wonderful windows manager.
-- 
Andrea De Michele



More information about the clfswm-devel mailing list