[racket] Choosing what class to instantiate at runtime

From: Kieron Hardy (kieron.hardy at gmail.com)
Date: Wed Jun 27 12:39:37 EDT 2012

Thanks Danny, that's a step in the direction I wanted to go.

But on further reflection, I think what I was looking for (i.e. have some class that stops it's own instantiation and defers to another class depending on some parameter value(s)) doesn't really make sense. Instead I need some sort of factory function that contains the conditionals and that returns an instance of the right class.

Cheers,

Kieron

On Jun 26, 2012, at 8:22, Danny Yoo <dyoo at hashcollision.org> wrote:

>> The solution that springs to mind is to choose which class to instantiate
>> with (if (list? param-a) ,,,) but then I'd have to state the parameter lists
>> twice.
> 
> Hi Kieron,
> 
> 
> Can you choose the class using an if or cond?
> 
> For example:
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define (try-it x)
> 
> (define the-class%
>   (if (list? x) multi-item-representer% item-representer%))
> 
> (define r (new the-class% [param-a x] [param-b 50] [param-c "yellow"]))
> 
> (send r print))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> This bit of code binds the-class% at runtime to one of those classes,
> depending on x.



Posted on the users mailing list.