This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

OOP and Relationship to the Guile Module System


I have been learning Guile and especially the module
system.  It struck me that it is much like an object
system.  The emphasis the point I have included the
following code.  My questions are:

Is it feasible to use the module system for this
purpose.  Is it to heavy-handed?  Is there too
much overhead involved?  Could the module
system be lightened up if it is to heavy?

Also could a procedure like eval-in-module
be created so that  it can evaluate in more
than one module at a time?  This is in 
regards to the methods (procs) defined in
one module to reference slots (vars) in a
different module.  This would be a kind of
merging of two modules but only for the life
of the eval.  This would allow one to temporaily
plug in an instance module to a class module
for evaluation.

(define define-generic-method
  (procedure->macro
   (lambda (exp env)
     `(define
	,(cadr exp)
	(lambda (obj . args)
	  (let ((proc (eval-in-module ',(cadr exp) obj)))
	    (apply proc (cons obj args))))))))

(define (eval-all-in-module elist m)
  (if (null? elist)
      #t
      (begin 
	(eval-in-module (car elist) m)
	(eval-all-in-module (cdr elist) m))))

(define make-object
  (procedure->macro
   (lambda (exp env)
     `(let ((%%object%% (make-module 16 (append ,(cadr exp) (list
the-root-module)))))
	(eval-all-in-module ',(cddr exp) %%object%%)
        %%object%%))))

(define make-class make-object)
(define class? module?)
(define instance? module?)
(define slot-ref module-ref)
(define method-ref module-ref)
(define slot-set! module-set!)
(define slot-define! module-define!)
(define-generic-method initialize-instance)
(define-generic-method describe)

(define (make-instance class . inits)
  (let ((inst (make-object (list class))))
    (apply initialize-instance (cons inst inits))
    inst))

(define person
  (make-class '()
     (define instance-slots '(name age height weight))
     (define all-persons '())
     (define (describe obj)
       (map (lambda (slot) (slot-ref obj slot)) '(name age height
weight)))
     (define initialize-instance
       (lambda (inst name age height weight)
	 (slot-define! inst 'name name)
	 (slot-define! inst 'age age)
	 (slot-define! inst 'height height)
	 (slot-define! inst 'weight weight)))))

(define wade (make-instance person 'wade 38 171 70))
(define arline (make-instance person 'arline 38 173 60))

(describe wade)
(describe arline)