Skip to content

Instantly share code, notes, and snippets.

@bishboria
Created April 26, 2011 21:30
Show Gist options
  • Save bishboria/943213 to your computer and use it in GitHub Desktop.
Save bishboria/943213 to your computer and use it in GitHub Desktop.
OO in Scheme
;; All the OO stuff
(define (object selector-names . all-selectors)
(define (apply-it selector selector-names selectors parameters)
(cond ((null? selectors) '(I cant do that Dave!))
((equal? selector (car selector-names)) (apply (car selectors) parameters))
(#t (apply-it selector (cdr selector-names) (cdr selectors) parameters))))
(lambda (selector . parameters)
(apply-it selector selector-names all-selectors parameters)))
;; Defining a new object type
(define (person name age)
(object '(name age)
(lambda ()
name)
(lambda ()
age)))
;; Usage
(define someone (person 'stu 28))
(someone 'name)
(someone 'age)
(define (maths)
(object '(add)
(lambda (a b)
(+ a b))))
((maths) 'add 3 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment