[racket] Help: Passing a class to a macro and not an identifier

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Sep 15 10:17:32 EDT 2010

Now see Robby's answer. 

On Sep 15, 2010, at 10:09 AM, Laurent wrote:

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



Posted on the users mailing list.