(require 'stdlib) (require 'rep.data.objects) ;; NOTE: there's no notion of classes and instances. ;; single inheritance (define Base (object nil ((name) "Base") ((add a b) (+ a b)))) (define Derived (object Base ((name) "Derived") ((mul a b) (* a b)))) (puts (Base 'name)) (puts (Base 'add 3 4)) (puts (Derived 'name)) (puts (Derived 'add 3 4)) (puts (Derived 'mul 3 4)) ;; TODO uniform access principle: properties and methods (define UAP (object nil ((initialize) (puts "initialize") (let ((name "nil")))) ((name) name) ((name= n) (setq name n)) ((foo) (progn (puts "foo") (self 'bar))) ((bar) (puts "bar")))) ;;(puts (UAP 'name)) ;;(UAP 'name= "Arie") ;;(puts (UAP 'name)) ;;(UAP 'initialize) ;;(UAP 'foo) ;;(puts (UAP 'name)) ;; how to capture state in an object (let ((stack ())) (define stack (object nil ((new) (setq stack ()) (setq foo 'bar)) ; setq foo here.. ((push value) (puts foo) ; ..and its still bound here (setq stack (cons value stack))) ((pop) (prog1 (first stack) (setq stack (rest stack))))))) (stack 'new) (stack 'push 42) (stack 'push 11) (puts (stack 'pop)) (puts (stack 'pop))