;;; This is some sample Oaklisp code ;;; New type, generic animal ;;; Handles weight at this level. Weight is accessible using settable ;;; operation WEIGHT, and incremented by GROW! (define animal (make type '(w) (list object))) (add-method (print (animal w) self stream) (format stream "#" (object-hash self) w)) ;;; Animals start weighing nothing (add-method (initialize (animal w) self) (set! w 0) self) ;;; Define accessor (define weight (make settable-operation)) (add-method (weight (animal w) self) w) (add-method ((setter weight) (animal w) self new-w) (set! w new-w)) ;;; Define GROW! (define grow! (make operation)) (add-method (grow! (animal w) self) (set! w (+ w 1))) ;;; Define animal hierarchy (define vertebrate (make type '() (list animal))) (define backbone? (make operation)) (add-method (backbone? (animal) self) #f) (add-method (backbone? (vertebrate) self) #t) (define fish (make type '() (list vertebrate))) (define amphibian (make type '() (list vertebrate))) (define frog (make type '() (list amphibian))) (define mammal (make type '() (list vertebrate))) (define pig (make type '() (list mammal))) (define human (make type '() (list mammal))) (define sound (make operation)) (add-method (sound (frog) self) 'ribbit) (add-method (sound (pig) self) 'oink) (add-method (sound (human) self) 'blah-blah-blah) (add-method (grow! (pig) self) (set! (weight self) (+ (weight self) 50))) (define kermit (make frog)) (set! (weight kermit) 40) (define miss-piggy (make pig)) (set! (weight miss-piggy) 210) (define legs (make operation)) (add-method (legs (vertebrate) self) 4) (add-method (legs (fish) self) 0) (add-method (legs (human) self) 2) ;;; > kermit ;;; # ;;; ;;; > (sound kermit) ;;; RIBBIT ;;; ;;; > (grow! kermit) ;;; 41 ;;; ;;; > kermit ;;; # ;;; ;;; > miss-piggy ;;; # ;;; ;;; > (grow! miss-piggy) ;;; 260 ;;; ;;; > miss-piggy ;;; #