[racket] inherit, inherit/super, and inherit/inner in class syntax
On 2013-06-14 14:39:57 -0600, Christopher wrote:
> So I have been trying to learn the ins and outs of Racket's class
> system. I've a little puzzled when it comes to the "inherit"-forms.
> I've poured over the Racket Reference and fiddled with some
> experimental classes, but I'm still not clear.
>
> My questions specifically are, What is the difference between inherit
> and inherit/super, and how does inherit/inner work?
Here's an example that hopefully shows the difference:
#lang racket
(define point%
(class object%
(inspect #f) ; to make example object easier to understand
(super-new)
(init-field [x 0] [y 0])
(define/public (move-x dx)
(new this% [x (+ x dx)] [y y]))))
(define fast-point%
(class point%
(super-new)
(inherit/super move-x)
;; or you can inherit
;(inherit move-x)
(define/public (move-fast dx)
;; only with inherit/super or override
(super move-x (* dx 10))
;; with inherit, inherit/super, or override
;(move-x (* dx 10))
)))
(send (new fast-point% [x 0] [y 2]) move-fast 3)
Notice that with `inherit/super`, you can use `super` on the method name
that you inherit from the superclass. Normally, you can only call
`super` on a method name that you are overriding.
On the other hand, with either `inherit` or `inherit/super`, you can
call the superclass method by just using the name.
I have never found the need to use `inherit/super` or `inherit/inner` in
my programs though. I always use `inherit`.
(If you wanted to know the rationale of why `inherit/super` exists, I'm
not sure. The commit log says it was added to replace `rename-super`
eventually)
Cheers,
Asumu