unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: David Pirotte <david@altosw.be>
To: Andrew Erlanger <andrew.erlanger@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: Problem merging generics across modules
Date: Fri, 13 Oct 2017 11:12:42 -0300	[thread overview]
Message-ID: <20171013111242.3b23f413@capac> (raw)
In-Reply-To: <8760bjncez.fsf@ateguix.i-did-not-set--mail-host-address--so-tickle-me>

[-- Attachment #1: Type: text/plain, Size: 2403 bytes --]

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

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  parent reply	other threads:[~2017-10-13 14:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-13  0:01 Problem merging generics across modules Andrew Erlanger
2017-10-13  0:03 ` Andrew Erlanger
2017-10-13 14:12 ` David Pirotte [this message]
2017-10-13 15:14   ` David Pirotte
2017-10-13 14:35 ` David Pirotte
     [not found]   ` <874lqxbgnf.fsf@ateguix.i-did-not-set--mail-host-address--so-tickle-me>
2017-10-17 23:23     ` David Pirotte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171013111242.3b23f413@capac \
    --to=david@altosw.be \
    --cc=andrew.erlanger@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).