[racket] public, override -- how to combine

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat May 24 04:11:39 EDT 2014

At Sat, 24 May 2014 10:16:34 +0400, Roman Klochkov wrote:
>  Is there a way to make C++ way inheritance: if superclass has the method, then 
> override, else create new?
> 
> I see in manual, that public requires " method must not be present already in 
> the superclass" and override requires "definition already present in the 
> superclass".
> 
> Is it possible to not restrict the superclass?

There's nothing built in (at the same level as `public` and `override`)
to do that.

You could use `method-in-interface?` to check whether a superclass has
a method already and add the method if not:

 (define (fixup-for-m super%)
   (if (method-in-interface? 'm (class->interface super%))
       super%
       (class super%
         (super-new)
         (define/public (m . args) (void)))))

 (class (fixup-for-m super%)
   ....
   (define/override (m ....) ....)
   ....)



Posted on the users mailing list.