guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

accessor with 2/3 parameters


From: Marco Maggi
Subject: accessor with 2/3 parameters
Date: Fri, 18 Aug 2006 20:35:45 +0200

Ciao,

  I wanted to create an accessor called ELM with synopsis:

  (set! (elm obj key) value)
  (elm obj key)

which is the same required by the VECTOR-SET! and
VECTOR-REF pair. For this  both  the #:accessor
keyword of class definition and the explicit
definition:

  (define-method (elm (obj <my-class>) key) ...)
  (define-method ((setter elm) (obj <my-class>) ...) ...)

do not work. I had to use:

  (export elm)
  (set! elm (ensure-accessor
             (make-procedure-with-setter
              (lambda (o key)
                ...)
              (lambda (o key value)
                ...))))

So a working example is:

;; ----------------------------------------

(use-modules (oop goops))

(debug-enable 'debug)
(debug-enable 'backtrace)
(debug-enable 'trace)


(define-class <my-vec> ()
  (v #:init-keyword #:value))

(define elm (ensure-accessor
             (make-procedure-with-setter
              (lambda (v k)
                (vector-ref (slot-ref v 'v) k))
              (lambda (v k value)
                (vector-set! (slot-ref v 'v) k value)))))

(define-method (display (o <my-vec>) (port <port>))
  (display (slot-ref o 'v) port))

(define v (make <my-vec> #:value #(1 2 3)))

(set! (elm v 1) -9)
(display v)(newline)
(display (elm v 0))(newline)
(display (elm v 1))(newline)
(display (elm v 2))(newline)

;; ----------------------------------------

  I do not like it because ELM does not know about
<my-vec>, but at  least ELM is a  generic function.
Is there a better way to do it?

  If there is a solution it could be a good idea to
add the implementation example to the GOOPS
documentation.

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"





reply via email to

[Prev in Thread] Current Thread [Next in Thread]