[racket] public, override -- how to combine

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Sat May 24 09:16:26 EDT 2014

 Ok, so 

(define (printable %)
   (cond
     [(implementation % printable<%>) %] ;; already have
     [(method-in-interface? 'print (class->interface %)) ;; have another print, should override
      ((mixin () (printable<%>) (define/override (print) (displayln this))) %)
     [else ;; no method, add
       ((mixin () (printable<%>) (define/public (print) (displayln this))) %)))

Without second clause (method-in-interface? ....), I cannont make an implemetation of printable<%> with given superclass.


Sat, 24 May 2014 12:22:20 +0200 от Laurent <laurent.orseau at gmail.com>:
> 
>>Another example is a mixin. Suppose I have an interface printable<%> = (interface () print)) and I want to make a mixin printable, that should make any class to implement printable.
>>The only solution is:
>>
>>(define (printable %)
>>   (if (method-in-interface? 'print (class->interface %))
>>       %
>>       ((mixin () (print) (define (print) (displayln this))) %)))
>
>Here too, and the new class should implement `printable<%>` instead of just implementing the method.
>Then to know if you can call `print` on a given object, you should first check that it implements `printable`.
>
>Checking for the implementation of the right interface ensures that the method does what you think it should do; because maybe someone has defined another interface `my-printable<%>` that also declares `print` but has a quite different purpose from `printable<%>`.
>
>Maybe I'm  totally missing something here, but the only correct use I see to `method-in-interface?` is in Racket's implementation of the class system, to check for errors if the method is called but may not exist.
>Laurent


-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140524/5234d0ce/attachment.html>

Posted on the users mailing list.