[racket] public, override -- how to combine

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Sat May 24 12:47:45 EDT 2014

 OK. I agree, that in common case it is not good. 


Sat, 24 May 2014 15:43:17 +0200 от Laurent <laurent.orseau at gmail.com>:
>That doesn't sound right to me.
>In the second clause, there already is a `print` method from some superclass, and this `print` method quite probably has a purpose, possibly not related to `printable<%>`, and you may not know what it is for.
>But here this behavior is completely overridden for `printable<%>`, which means that if the method is used for its original purpose (which you may not be able to control, e.g., on instantiation) the program may crash.
>If you do have control on what this original `print` is, then probably it should be renamed to avoid name collision.
>
>Here I think the correct behavior is to throw an error saying that the class cannot be made `printable<%>` (or maybe implement an `printable2<%>`, but that too looks like bad duct tape).
>
>Laurent
>
>On Sat, May 24, 2014 at 3:16 PM, Roman Klochkov  < kalimehtar at mail.ru > wrote:
>>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
>


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

Posted on the users mailing list.