[plt-scheme] detecting duplicate internal definitions

From: Doug Orleans (dougo at place.org)
Date: Tue May 11 13:19:17 EDT 2004

Eli Barzilay writes:
 > Thanks.  However, changing this doesn't work -- the second `defmethod'
 > gets a syntax-local-context of 'expression...

I've found this macro useful for making expressions usable in
internal-definition position (or anywhere else):

  ;; Helper macro for making an expression usable in any lexical context.
  (define-syntax (define-dummy stx)
    (syntax-case stx ()
      ((_ sym exp)
       (if (list? (syntax-local-context))
	   ;; Internal definition position; generate a dummy binding.
	   #`(define #,(car (generate-temporaries #'(sym))) exp)
	   ;; Otherwise, don't clutter the namespace.

Then both `defmethod's will turn into `define's, but neither will
define a generic.

 > *sigh*, as I said, I don't think that this is too reliable, but I
 > tried something that seems like it might work.  The idea is to keep a
 > mapping from the definition context to things that were already
 > defined as generics.

Heh, I was going to suggest this idea, but I thought it was too crazy
to bring up.  :)

--dougo at place.org

P.S. Slightly different topic, but is it possible to use the new
`normalize-definition' function from (lib "define.ss" "syntax") to
allow things like this?

    (defmethod ((f x y) z) (+ z (- x y)))
    ((f 5 3) 2) ; => 4

I think this won't work for arguments with specializers, though,
because it checks that each argument is an identifier.  But perhaps
that part could be factored out.

Posted on the users mailing list.