define-subclass macro
Posted: Thu Oct 15, 2009 9:26 pm
While working on the Dragonfly framework I came up with a useful macro called define-subclass for FOOP:
It must be called while in the MAIN context. Here's an example of how it's used:
Neat huh? :-)
Code: Select all
(define-macro (define-subclass)
(new (args 0 1) (args 0 0))
(dolist (method (rest $args))
(setf (method 0 0) (sym $it (args 0 0)))
(eval (push 'define method))
)
)
Code: Select all
(new Class 'Foo)
(define (Foo:get x) (x 1))
(define (Foo:set x v) (setf (x 1) v) x)
(define-subclass (Bar Foo)
((get x) (x 2))
((set x v) (setf (x 2) v) x)
((str x) (string x))
)
(:get (Foo 1 2)) => 1
(:get (Bar 1 2)) => 2
(:str (:set (Bar 1 2) 3)) => (Bar 1 3)