unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* accessor with 2/3 parameters
@ 2006-08-18 18:35 Marco Maggi
  2006-08-19 10:29 ` Neil Jerram
  0 siblings, 1 reply; 4+ messages in thread
From: Marco Maggi @ 2006-08-18 18:35 UTC (permalink / raw)


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"



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: accessor with 2/3 parameters
@ 2006-08-20  6:08 Marco Maggi
  0 siblings, 0 replies; 4+ messages in thread
From: Marco Maggi @ 2006-08-20  6:08 UTC (permalink / raw)


"Neil Jerram" wrote:
>Well, the following seems to work ...
>[...]
>In other words, it appears that the args of
>make-procedure-with-setter
>can be generics.

Yes! The following appears to do what I want:

(use-modules (oop goops))

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

(define-method (my-vec-setter (v <my-vec>) k)
  (vector-ref (slot-ref v 'v) k))

(define-method (my-vec-getter (v <my-vec>) k value)
  (vector-set! (slot-ref v 'v) k value))

(define elm (make-procedure-with-setter
              my-vec-setter my-vec-getter))

(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)

Thank You!

My real code does not act upon Guile vectors but upon
a custom class that wraps a custom SMOB. I require the
generic-ness of ELM to let other Guile extensions take
it in an attempt to write math functions 'compatible'
with Guile-GSL.

--
Marco Maggi

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



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


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

end of thread, other threads:[~2006-08-20  6:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-18 18:35 accessor with 2/3 parameters Marco Maggi
2006-08-19 10:29 ` Neil Jerram
2006-08-19 23:17   ` Neil Jerram
  -- strict thread matches above, loose matches on Subject: below --
2006-08-20  6:08 Marco Maggi

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).