<br><br><div class="gmail_quote">On Wed, Sep 15, 2010 at 16:17, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
Now see Robby's answer.<br></blockquote><div><br>I have, several times, but I must be blind.<br>The code I gave works for class a%, so certainly a% can be "inspected" by<br>(interface->method-names (class->interface a%))<br>
during the expansion of the macro, and it generates the functions names<br>according to the method names, so that their existence is detected at compile <br>time (thanks to datum->syntax).<br>I suspect this is why the last line '(get-the-val)' in the module doesn't raise <br>
a syntax error.<br><br>The only remaining problem I have is to abstract class->singleton over a%,<br>so that the class can be given as an argument to the macro.<br><br>Ah, in fact I've just found a (probably ugly) workaround that I hope to improve<br>
(see below):<br>To abstract over a%, I just turned class->singleton into a macro-maker<br>that takes a class (at compile time) and instantiate define-class->singleton<br>for a given class.<br><br>I am a bit puzzled since this does not match your (kind) answers so I must<br>
have either badly explained my problem, or badly understood your answers <br>(or both).<br><br>Anyway, thanks a lot for your help.<br><br>Laurent<br>P.S.: If anyone knows how to improve this, I'm interested.<br><br> <br>
<br><singleton.rkt><br>#lang racket<br><br>(require (for-syntax racket/class))<br><br>(define-syntax-rule (define-singleton-sender func obj meth)<br> (begin (printf "define-sender: ~s ~s ~s\n" 'func 'obj 'meth)<br>
(define (func . args)<br> (send/apply (obj) meth args))))<br><br>(define-syntax-rule (define-class->singleton define-name cl)<br> (define-syntax (define-name stx)<br> (syntax-case stx ()<br> [(_ obj)<br>
(with-syntax ([(name (... ...))<br> (map (ë(n)(datum->syntax #'obj n))<br> (interface->method-names (class->interface cl)))])<br> #'(begin (define-singleton-sender name obj name)<br>
(... ...)))])))<br><br>; Tests<br><br>(require "a.rkt"<br> (for-syntax "a.rkt"))<br><br>; Instantiate the macro for a% :<br>(define-class->singleton a%->singleton a%)<br>
<br>(define current-a (make-parameter (new a% [val 3])))<br><br>(a%->singleton current-a)<br><br>(get-the-val)<br><br>-><br>define-sender: get-the-val current-a get-the-val<br>3<br><br><br></div></div>