* 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-18 18:35 accessor with 2/3 parameters Marco Maggi
@ 2006-08-19 10:29 ` Neil Jerram
2006-08-19 23:17 ` Neil Jerram
0 siblings, 1 reply; 4+ messages in thread
From: Neil Jerram @ 2006-08-19 10:29 UTC (permalink / raw)
Cc: guile-user
"Marco Maggi" <marco.maggi-ipsu@poste.it> writes:
> 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)
Do you need the `ensure-accessor' wrapper in order for this example to
work? I suspect not.
In other words, I think you should get the same result with
(define elm (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))))
> I do not like it because ELM does not know about
> <my-vec>, but at least ELM is a generic function.
But you don't actually use the generic-ness here, do you?
> Is there a better way to do it?
Not that I know of. I'll take a look at the code though.
Regards,
Neil
_______________________________________________
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-19 10:29 ` Neil Jerram
@ 2006-08-19 23:17 ` Neil Jerram
0 siblings, 0 replies; 4+ messages in thread
From: Neil Jerram @ 2006-08-19 23:17 UTC (permalink / raw)
Cc: guile-user
Neil Jerram <neil@ossau.uklinux.net> writes:
> Not that I know of. I'll take a look at the code though.
Well, the following seems to work ...
(use-modules (oop goops))
(define-method (second (l <list>)) (cadr l))
(define-method (set-second (l <list>) val) (set-car! (cdr l) val))
(define-method (second (l <vector>)) (vector-ref l 1))
(define-method (set-second (l <vector>) val) (vector-set! l 1 val))
(define sec (make-procedure-with-setter second set-second))
(sec '(1 2 3)) => 2
(sec #(1 2 3)) => 2
(define l (list 'a 'b 'c))
(set! (sec l) 'fff)
l => (a fff c)
(define l (vector 'a 'b 'c))
(set! (sec l) 'fff)
l => #(a fff c)
In other words, it appears that the args of make-procedure-with-setter
can be generics.
Regards,
Neil
_______________________________________________
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).