[plt-scheme] how to abstract over (new ...) expressions?

From: samth (samth at ccs.neu.edu)
Date: Fri May 5 12:56:07 EDT 2006

On Fri, 2006-05-05 at 12:04 -0400, Richard Cobbe wrote:
> How does one abstract over (new ...) expressions?
> 
> Here's the situation:
> 
> (define-struct base (x))
> (define-struct (A base) ())
> (define-struct (B base) (y))
> 
> (define A%
>   (class object%
>     [init-field fd1]
>     ...))
> 
> (define B%
>   (class object%
>     [init-field fd1]
>     [init-field fd2]
>     ...))
> 
> I need to convert instances of A and B into instances of A% and B%
> respectively:
> 
> (define (convert st)
>   (if (A? st)
>     (new A% [fd1 (f (base-x st))])
>     (new B% [fd1 (f (base-x st))]
>             [fd2 (g (B-y st))])))

Would something like this work for you?

(define (convert st)
  (converter st A? A% B% [(fd1 f base-x)] [(fd2 g B-y)]))

where converter is:

(define-syntax converter
  (syntax-rules ()
    [(_ val pred? fst-obj snd-obj [(fld fun sel) ...] [(fld* fun* sel*) ...])
     (if (pred? val)
         (new fst-obj [fld (fun (sel val))] ...)
         (new snd-obj [fld (fun (sel val))] ... [fld* (fun* (sel* val))] ...))]))

I haven't tried using this, only expanding it, but I think it will work.

sam th




Posted on the users mailing list.