This is the mail archive of the
kawa@sourceware.cygnus.com
mailing list for the Kawa project.
OOP in Kawa
- To: kawa at sourceware dot cygnus dot com
- Subject: OOP in Kawa
- From: Luke Gorrie <luke at bluetail dot com>
- Date: 19 May 2000 09:54:59 +0200
Hi all,
I've been doing a lot of object-oriented Kawa hacking lately, and I've
been using a whole lot of different ways of representing objects in my
search for something really good. Mutable records with regular scheme
functions for implementing methods lacks polymorphism and leaves me
open to method-name-clashes, scheme-traditional objects as closures
dispatching on symbol are cumbersome (though I bet a nice macro could
make it nice) and doesn't play nicely with java, and (object ..)
annoys me when it comes to accessing instance fields.
Anyway I've got something that's working nicely which I thought I'd
pass on - its a closure/object combo which uses a scheme environment
for storing instance fields in and uses (object ...) for implementing
methods. eg:
(define counter
(create-object
;; superclasses and interfaces
(<object>)
;; list of instance fields and initial values
((count 0))
;; methods - same syntax as (object ..)
((increment) <void>
(set! count (+ count 1)))
((get-count) <object>
count)))
(invoke counter 'get-count) => 0
(invoke counter 'increment) => void
(invoke counter 'get-count) => 1
This is working really nicely for me at the moment, thought I'd pass
on the tip! Here's my create-object macro.
(defmacro create-object (parents variables . methods)
(define (define-variable v)
`(define ,(car v) ,(cadr v)))
(let ((obj `(object ,parents ,@methods)))
`((lambda ()
(define $this ,obj)
,@(map define-variable variables)
$this))))
Note: $this is bound to the object itself in all the methods.
Constructor is lacking but hasn't been a problem for me as I tend to
create objects from a factory function which can perform the
initialisation.
Interested to hear what others are using!
Cheers,
Luke