unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Simplified slot access in goops
@ 2008-11-27 20:36 Maciek Godek
  2008-11-28 18:41 ` Panicz Maciej Godek
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Maciek Godek @ 2008-11-27 20:36 UTC (permalink / raw)
  To: guile-user

Hi,
Below is a simple set of functions and macros to make
the access to goops object properties easier:

(use-modules (oop goops))
(use-syntax (ice-9 syncase))

(define-syntax let-alias
  (syntax-rules ()
    ((_ ((id alias) ...) body ...)
     (let-syntax ((helper (syntax-rules ()
			    ((_ id ...) (begin body ...)))))
       (helper alias ...)))))

(define slot (make-procedure-with-setter slot-ref slot-set!))

(define-syntax in (syntax-rules ()
		    ((_ object expr ...)
		     (primitive-eval
		      (list 'let-alias
			    (map (lambda(s)(list(car s)(list 'slot object (list 'quote (car s)))))
				 (class-slots (class-of object)))
			    (quote expr) ...)))))

Now suppose we have an instance of a class:
(define-class C ()
  a b c)
(define o (make C))

Using the "in" macro, we can write:
(in o
  (set! a 5)
  (set! b (1+ a))
  (set! c (+ a b))
  (+ a b c))
=> 22

which is more convenient than:
(slot-set! o 'a 5)
(slot-set! o 'b (1+ (slot-ref o 'a))
(slot-set! o 'c (+ (slot-ref o 'a) (slot-ref o 'b)))
(+ (slot-ref o 'a) (slot-ref o 'b) (slot-ref o 'c))

Perhaps the possible inconvenience is that all variable
names that happen to be the slot names of a given class
are shadowed. In the long run it may also cause significant
performance problems (or that's what I think), especially
when dealing with objects with a hell lotta slots.

I wonder if it would be possible to make it optimizable
for the JIT compiler somehow (and to make the "in" syntax
official part of GOOPS)

Sincerely,
M.




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-08-29  0:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-27 20:36 Simplified slot access in goops Maciek Godek
2008-11-28 18:41 ` Panicz Maciej Godek
2008-12-01 22:25 ` Neil Jerram
2008-12-02  9:32   ` Clinton Ebadi
2008-12-02 19:30     ` Panicz Maciej Godek
2008-12-02 20:33       ` Clinton Ebadi
2010-08-28 20:07 ` Andy Wingo
2010-08-29  0:22   ` Panicz Maciej Godek
2010-08-29  0:56     ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).