[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem merging generics across modules
From: |
David Pirotte |
Subject: |
Re: Problem merging generics across modules |
Date: |
Fri, 13 Oct 2017 11:12:42 -0300 |
Hello Andrew,
> I am trying to extend the definition of a few primitives,
> including equal?, in a project of mine. Let's say that a.scm contains:
> (define-module (project a)
> #:use-module (oop goops)
> #:export (equal?)
When you extend a guile primitive, don't #:export it in your module definition,
because that creates a new binding, hiding guile's core primitive. You should
use
#:re-export instead, though in this very specific case of extending a guile core
primitive it is not even necessary, because when guile 'encounter' your
define-method, it does this:
it creates a generic function for the method name, if it does not
already
exists, by calling define-generic [1]:
define-generic checks that the name was (or not) previously
bound to
a scheme procedure, in which case it incorporates it into the
new
generic function as its default procedure
it adds your methods to the generic function...
David
[1] unless you really know what you are doing, you don't want to call
define-generic yourself, because if it existed already, then that
previous
generic function would discarded and replaced by a new, empty generic
function. goops calls define-generic for you, as part ofthe
define-method
protocol, iff there is no generic function for that name, so you are
always
on the safe side to rely on goops here.
;;;
;;; Extending equal?
;;;
(define-module (extending-equal)
#:use-module (oop goops)
#:export (<color>
!r
!g
!b))
(define-class <color> ()
(r #:accessor !r #:init-keyword #:r #:init-value 0)
(g #:accessor !g #:init-keyword #:g #:init-value 0)
(b #:accessor !b #:init-keyword #:b #:init-value 0))
(define-method (equal? (c1 <color>) (c2 <color>))
(pk "in extended equal? ...")
(and (= (!r c1) (!r c2))
(= (!g c1) (!g c2))
(= (!b c1) (!b c2))))
;;;
;;; Let's try it
;;;
GNU Guile 2.2.2.3-0c102
scheme@(guile-user)> (add-to-load-path (getcwd))
scheme@(guile-user)> ,use (oop goops)
scheme@(guile-user)> ,use (extending-equal)
;;; note: source file /usr/alto/projects/guile/goops/extending-equal.scm
;;; ...
scheme@(guile-user)> (make <color>)
$2 = #<<color> 5610ca6b2ba0>
scheme@(guile-user)> (make <color>)
$3 = #<<color> 5610ca7480f0>
scheme@(guile-user)> (equal? $2 $3)
;;; ("in extended equal? ...")
$4 = #t
pgp5rvgD_Hc8s.pgp
Description: OpenPGP digital signature