clos generics in a library

1 view
Skip to first unread message

Eduardo Cavazos

unread,
Apr 28, 2011, 3:49:57 AM4/28/11
to Mosh Developer Disscus
Hello,

Currently, it seems that if you want to define generics and methods in
a library, you need to structure the library such that the generic
definitions all appear before the method definitions. For example,
importing this library:

(library (test-clos)

(export a b)

(import (rnrs)
(clos user)
(clos bootstrap standard-classes))

(define-generic a)

(define-method a ((n <number>))
n)

(define-generic b)

(define-method b ((n <number>))
(* n n))

)

results in an error:

mosh> (import (test-clos))

Unhandled exception:

Condition components:
1. &who who: define
2. &message message: "a definition was found where an
expression was expected"
3. &syntax form: (define b (make <generic> 'definition-name
'b))
subform: #f

This version however works:

(library (test-clos)

(export a b)

(import (rnrs)
(clos user)
(clos bootstrap standard-classes))

(define-generic a)

(define-generic b)

(define-method a ((n <number>))
n)

(define-method b ((n <number>))
(* n n))

)

This is of course because the 'define-method' macro doesn't expand
into a definition, but an expression. Perhaps as a workaround, the
macro could expand into something like:

(define NO-OP expression)

Ed

Eduardo Cavazos

unread,
Apr 28, 2011, 3:35:45 PM4/28/11
to Mosh Developer Disscus
This change seems to do the trick:

/usr/local/share/mosh/0.2.6/lib/clos $ diff _user.ss user.ss
77,97c77,98
< #`(add-method #,generic
< (make <method>
< 'specializers (list #,@types)
< 'qualifier '#,qualifier
< 'procedure
< (lambda (%generic %next-methods #,@qargs ?
arg ... . ?rest)
< (let ((#,call-next-method
< (lambda ()
< (if (null? %next-methods)
< (apply error
< 'apply
< "no next method"
< %generic
< #,@qargs ?arg ... #,rest-
args)
< (apply (car %next-methods)
< %generic
< (cdr %next-methods)
< #,@qargs ?arg ... #,rest-
args))))
< (next-method?
< (not (null? %next-methods))))
< . #,body))))))))
---
> #`(define no-op
> (add-method #,generic
> (make <method>
> 'specializers (list #,@types)
> 'qualifier '#,qualifier
> 'procedure
> (lambda (%generic %next-methods #,@qargs ?arg ... . ?rest)
> (let ((#,call-next-method
> (lambda ()
> (if (null? %next-methods)
> (apply error
> 'apply
> "no next method"
> %generic
> #,@qargs ?arg ... #,rest-args)
> (apply (car %next-methods)
> %generic
> (cdr %next-methods)
> #,@qargs ?arg ... #,rest-args))))
> (next-method?
> (not (null? %next-methods))))
> . #,body)))))))))
110c111
< ) ;; library (clos user)
---
> )
\ No newline at end of file
Reply all
Reply to author
Forward
0 new messages