Object Systems
Portable (Kiczales); basis for several systems Racket classes
Racket (class, send, interfaces, mixins, traits) Closure-based objects Portable (Module 23) R6RS/Chez records with inheritance Lightweight data inheritance
;; GOOPS (Guile Object-Oriented Programming System): CLOS-like
(use-modules (oop goops))
(define-class <shape> ()
(name #:init-keyword #:name #:getter shape-name #:init-value "shape"))
(define-class <circle> (<shape>)
(radius #:init-keyword #:radius #:accessor radius))
(define-class <rect> (<shape>)
(width #:init-keyword #:width #:getter width)
(height #:init-keyword #:height #:getter height))
(define-generic area)
(define-method (area (c <circle>)) (* 3.14159 (radius c) (radius c)))
(define-method (area (r <rect>)) (* (width r) (height r)))
(define-method (describe (s <shape>))
(format #f "~a with area ~a" (shape-name s) (area s)))
(define shapes (list (make <circle> #:name "circle" #:radius 1)
(make <rect> #:name "rect" #:width 2 #:height 3)))
(map describe shapes) ; ("circle with area 3.14159" "rect with area 6")
;; Multiple dispatch
(define-method (collide (a <circle>) (b <rect>)) 'circle-hits-rect)
(define-method (collide (a <rect>) (b <circle>)) (collide b a))
;; next-method (like call-next-method)
(define-method (describe (c <circle>))
(string-append (next-method) " (round)"))