* Extending primitive to generic functions problem
@ 2020-10-09 18:30 Cor Legemaat
0 siblings, 0 replies; only message in thread
From: Cor Legemaat @ 2020-10-09 18:30 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 2611 bytes --]
Hi All:
I am trying to extend some of the primitive functions with generic
methods in a module to use them with custom classes. From 8.6.5 Generic
Function and Method Examples in the manual I came up with the following
module:
(define-module (test ep)
#:use-module (oop goops)
#:use-module ((rnrs base)
#:select (+ - * / map max min sqrt expt abs log)
#:prefix rnrs:))
(define-macro (make-primitive-generic source destination arguments)
(let ((tmp-symbol (string->symbol (string-append (symbol->string
destination)
"-new"))))
`(begin (define-generic ,tmp-symbol)
,(if (not (defined? destination))
`(define ,destination ,source))
(let ((,destination ,source))
,(cond
((eq? arguments 1)
`(define-method (,tmp-symbol (n <real>)) (,source n)))
((eq? arguments 2)
`(define-method (,tmp-symbol (a <real>) (b <real>))
(,source a b)))
(else
`(begin
(define-method (,tmp-symbol (a <real>) (b <real>))
(,source a b))
(define-method (,tmp-symbol (a <number>)) a)
(define-method (,tmp-symbol) 0)
(define-method (,tmp-symbol . args)
(,tmp-symbol (car args)
(apply ,tmp-symbol (cdr args))))))))
(set! ,destination ,tmp-symbol)
(export ,destination))))
(export make-primitive-generic)
(make-primitive-generic rnrs:+ sum 0)
(make-primitive-generic rnrs:max max 0)
(make-primitive-generic rnrs:min min 0)
(make-primitive-generic rnrs:* product 0)
(make-primitive-generic rnrs:/ divide 0)
(make-primitive-generic rnrs:- difference 0)
(make-primitive-generic rnrs:sqrt sqrt 1)
(make-primitive-generic rnrs:expt expt 2)
(make-primitive-generic rnrs:abs abs 1)
(make-primitive-generic rnrs:log log 1)
Now 2 problems:
If I don't hack the primitive with the prefix there is an 'Unbound
variable:' error on those where the source and destination is the same.
This works but only if compiled, the second time guile uses 100% cpu
and never finish loading. Did play with eval-when but it's either not
defining the methods or not loading after it's compiled.
Where is it going wrong, is this possible or do I mis understand
something about how guile works?
Thanks in advance
Regards:
Cor
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-10-09 18:30 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-09 18:30 Extending primitive to generic functions problem Cor Legemaat
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).