Hi, Using some hints you gave me, I've implemented a really tiny object system -- and I would like to know your opinion ("why it's still better to use goops" :D)
The notation for defining classes is following (using example of a sphere): (define sphere (class '(x y radius) '((move (dx dy) (set! x (+ x dx)) (set! y (+ y dy))) (scale (factor) (set! radius (* factor radius)))))) To make an instance, you simply use instance-of: (let ((S (instance-of sphere))) ... ) you can supply initial values to the object's props and call it's methods (let ((S (instance-of sphere 1 2 3))) (in S '(move 1 1)) ;move the sphere from (1 2) to (2 3) (in S '(scale 5)) (get S 'radius)) ; returns 15 The implementation is very simple: every object is a vector. Its first field is always a reference to the class, and the remaining fields are object's state (in the example, the values of x, y and radius of S) A class definition is a vector consisting of: hashmap N from property names to vector indices (in object) hashmap M from member function names to their indices in F vector F of member functions There's nothing surprising in here, and won't be. It has, as I can tell, a few advantages over goops -- mainly, storing objects as vectors allows for an efficient and convenient object treating from C level, so boatmen should be satisfied. Secondly, people accustomed to the object.method() notation (like myself) won't feel lost and the global namespace will be kept clean. The system certainly isn't as complex as goops and doesn't handle types (in general) so exquisitely. Also, it's unable to support multiple inheritance efficiently (single inheritance isn't supported as well, but this could be done quite easily if needed), but that's not my point. I just wanted to ask if you have any comments or remarks to share with (I know I'm not the first guy to implement a thing like this). I am currently using this system to implement another system (for networked objects -- I wrote about it in another post) and so far it caused no trouble. I attach the working implementation (if anyone's interested). For those who got this far (yes, that would be... you!), thanks for your attention :D M.
sos.scm
Description: Binary data