<br><br><div class="gmail_quote">On Wed, Sep 15, 2010 at 15:59, 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>
Where do you want to bind them? In the current namespace? Scope?<br></blockquote><div><br><br>Module scope, in the current namespace, runtime phase.<br>(I think, if I understand all these terms correctly).<br><br>Here is the same code as the previous one, but this time working (still only for a%):<br>
<br><a.rkt><br>#lang racket<br><br>(provide a%)<br><br>(define a%<br> (class object% (super-new)<br> (init-field val)<br> <br> (define/public (get-the-val)<br> val)<br> ))<br><br><br><singleton.rkt><br>
#lang racket<br><br>(require (for-syntax racket/class)<br> "a.rkt"<br> (for-syntax "a.rkt")<br> )<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 (class->singleton stx)<br> (syntax-case stx ()<br> [(_ cl obj)<br> (with-syntax ([(name ...)<br> (map (λ(n)(datum->syntax #'cl n))<br>
(interface->method-names (class->interface a%)))]) ; only for a% ***<br> #'(begin (define-singleton-sender name obj name)<br> ...))]))<br><br><br>(define current-a (make-parameter (new a% [val 3])))<br>
<br>(class->singleton a% current-a)<br><br>(get-the-val)<br><br><br><br>I really only need to pass some class to the class->singleton macro.<br>There must be some simple answer ; I'd be very surprised if this was not possible.<br>
<br>Thanks a lot for the time you take to answer.<br><br><br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br>
<br>
On Sep 15, 2010, at 9:47 AM, Laurent wrote:<br>
<br>
><br>
><br>
> On Wed, Sep 15, 2010 at 15:29, Matthias Felleisen <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>> wrote:<br>
><br>
><br>
> Would this help you:<br>
><br>
> #lang racket<br>
><br>
> (define a%<br>
> (class object%<br>
> (init-field this.x)<br>
> (define/public (foo x)<br>
> (set! this.x x))<br>
> (define/public (bar)<br>
> this.x)<br>
> (super-new)))<br>
><br>
> (new a% [this.x 10])<br>
><br>
> (define (generic-methods class%)<br>
> (map (lambda (name) (list name (make-generic class% name)))<br>
> (interface->method-names (class->interface class%))))<br>
><br>
> (generic-methods a%)<br>
><br>
> If I could pass the latter result to a macro that binds the names with the function automatically, yes,<br>
> but this seems to be exactly my initial problem, I think.<br>
><br>
><br>
><br>
><br>
><br>
> On Sep 15, 2010, at 9:10 AM, Laurent wrote:<br>
><br>
> ><br>
> ><br>
> > On Wed, Sep 15, 2010 at 14:57, Matthias Felleisen <<a href="mailto:matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>> wrote:<br>
> ><br>
> > Can we start from something simpler?<br>
> ><br>
> > The name of your macro suggests that you want something like this:<br>
> ><br>
> > (define one-and-only-one-instance<br>
> > (new (class my-super% ...)))<br>
> ><br>
> > This creates a class and creates a single instance. Because class is nested<br>
> > in the call to new, there is no way to get another instance.<br>
> ><br>
> > Would this be what you want?<br>
> ><br>
> > Okay, let me explain what I want to do (the name is not really set in stone yet).<br>
> ><br>
> > There is a class a% defined somewhere, and an instance parameter<br>
> > current-a%-object of class a%.<br>
> > Suppose a% has methods foo and bar.<br>
> ><br>
> > Then I call:<br>
> > (class->singleton a% current-a%-object)<br>
> ><br>
> > This is supposed to define the forms (foo ....) and (bar ....) so that they act like<br>
> > (send (current-a%-object) foo ....), etc.<br>
> ><br>
> > I want to build these definitions automatically from the methods of the class.<br>
> ><br>
> > Anyway, beside the intended goal, I'd still like to know how solve the macro issue.<br>
> ><br>
><br>
><br>
<br>
</div></div></blockquote></div><br>