[racket] public, override -- how to combine
On Sat, May 24, 2014 at 11:46 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> I'm making something like ORM. ORM class has methods `read' and `write'
> (implements interface orm<%>). But ORM class may have as a super either ORM
> class or simple class.
>
> And here I have a problem, that simple class doesn't implement interface
> orm<%>, so I have to introduce new methods,
> but if superclass is ORM class, then I have to override.
>
Then here you should use `implementation?` instead of
`method-in-interface?`, and in case the class does not yet implement
`orm<%>`, make it implement it, instead of just adding the method.
>
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140524/ce407df2/attachment.html>