Hi all,<br><br>I'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'd have to state the parameter lists twice.<br><br>It seems to me there'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 "hello world")<br>(define text-item-list (list "alpha bravo" "charlie delta" "echo foxtrot"))<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 "item:~a~n" 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 "multi-item:~n")<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 "red"]))<br>
(define r2 (new multi-item-representer% [param-a text-item-list] [param-b 20] [param-c "green"]))<br><br>(send r1 print)<br>(send r2 print)<br><br>;(define r (some-representer% [param-a x] [param-b 50] [param-c "yellow"]))<br>
;(send r print)<br><br>