[plt-scheme] "appending" to classes rather than extending

From: Doug Orleans (dougorleans at gmail.com)
Date: Thu Mar 6 00:17:32 EST 2008

Grant Rettke writes:
 > On Wed, Mar 5, 2008 at 9:47 PM, Doug Orleans <dougorleans at gmail.com> wrote:
 > >  You might consider Swindle
 > Ought one be a CLOS expert before getting started with Swindle?

Maybe if you want to do fancy things with the MOP, but otherwise it
should be pretty easy to pick up.  Below are some of the examples from
the PLT Scheme Guide chapter 13 Classes and Objects translated into

#lang swindle

(-defclass-auto-initargs- (:auto #t))   
(-defclass-autoaccessors-naming- :slot) 

(defclass <fish> () size)

(defmethod (grow (fish <fish>) (amt <number>))
  (set! (size fish) (+ amt (size fish))))

(defmethod (eat (fish <fish>) (other-fish <fish>))
  (grow fish (size other-fish)))

(define charlie (make <fish> :size 10)) ; this also works: (make-fish 10)

(grow charlie 6)
(echo (size charlie)) ; => 16

(defclass <hungry-fish> (<fish>))

(defmethod (eat-more (fish <hungry-fish>) (fish1 <fish>) (fish2 <fish>))
  (eat fish fish1) (eat fish fish2))

(defclass <picky-fish> (<fish>))

(defmethod (grow (fish <picky-fish>) (amt <number>))
  (call-next-method fish (* 3/4 amt)))

(define daisy (make <picky-fish> :size 20))

(eat daisy charlie)
(echo (size daisy)) ; => 32

(defclass <size-10-fish> (<fish>))

(defmethod (initialize (fish <size-10-fish>) initargs)
  (call-next-method fish (list :size 10)))

(echo (size (make <size-10-fish>))) ; => 10

(defclass <default-10-fish> (<fish>))

(defmethod (initialize (fish <default-10-fish>) initargs)
  (call-next-method fish (append initargs (list :size 10))))

(echo (size (make <default-10-fish>))) ; => 10
(echo (size (make <default-10-fish> :size 20))) ; => 20

--dougorleans at gmail.com

Posted on the users mailing list.