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

From: Richard Cobbe (cobbe at ccs.neu.edu)
Date: Thu May 11 20:50:32 EDT 2006

On Thu, May 11, 2006 at 08:34:21PM -0400, Matthias Felleisen wrote:
> How would this work:
> 
> (define-struct base (x))
> (define-struct (A base) ())
> (define-struct (B base) (y))
> 
> (define A%
>   (class object%
>     [init-field fd1]
>     (init (st #f))
>     (super-new)))
> 
> (define B%
>   (class object%
>     [init-field fd1]
>     (init (st #f))
>     [init-field (fd2 (B-y st))]
>     (super-new)))
> 
> (define (convert st)
>   (new (if (A? st) A% B%) [fd1 (base-x st)] [st st]))

Oh, ok -- you're essentially giving A% a dummy parameter to make the
abstraction work.  A little inelegant, but it works.

My situation is a bit tricker, because A% and B% are defined in someone
else's PLaneT package, so I can't set up their init args as you do
above.  But I could define my-A% to be subclass of A%, add the
additional init arg to my-A%, and create instances of my-A%.

As it turns out, I went with Sam's solution; I'll probably leave it at
that.

Thanks,

Richard


Posted on the users mailing list.