Hi all,<br><br>I&#39;d like to refactor and extend the code below, such that r is either defined to be an instance of an item-representer% or a multi-item-representer%, depending on whether param-a is a single item or a list. <br>
<br>The solution that springs to mind is to choose which class to instantiate with (if (list? param-a) ,,,) but then I&#39;d have to state the parameter lists twice.<br><br>It seems to me there&#39;s got to be a better way in Racket to do this. Thanks in advance for any suggestions on how to proceed.<br>
<br>Cheers,<br><br>Kieron.<br><br>****<br> <br>#lang racket<br><br>(define text-item &quot;hello world&quot;)<br>(define text-item-list (list &quot;alpha bravo&quot; &quot;charlie delta&quot; &quot;echo foxtrot&quot;))<br>
<br>(define x%<br>  (class object%<br>    (super-new)<br>    ))<br><br>(define item-representer%<br>  (class x%<br>    (init param-a param-b param-c)<br>    (super-new)<br>    (field (item param-a))<br>    (define/public (print)<br>
      (printf &quot;item:~a~n&quot; item))<br>    ))<br><br>(define multi-item-representer%<br>  (class x%<br>    (init param-a param-b param-c)<br>    (super-new)<br>    (field (item-list (map (lambda (item) (new item-representer% [param-a item] [param-b param-b] [param-c param-c])) param-a)))  <br>
    (define/public (print)<br>      (printf &quot;multi-item:~n&quot;)<br>      (map (lambda (item) (send item print)) item-list)<br>      )<br>    ))<br><br>(define r1 (new item-representer% [param-a text-item] [param-b 10] [param-c &quot;red&quot;]))<br>
(define r2 (new multi-item-representer% [param-a text-item-list] [param-b 20] [param-c &quot;green&quot;]))<br><br>(send r1 print)<br>(send r2 print)<br><br>;(define r (some-representer% [param-a x] [param-b 50] [param-c &quot;yellow&quot;]))<br>
;(send r print)<br><br>