racket/collects/tests/generics/coercion.rkt
Vincent St-Amour e7e66ce41c Implement coercion for method tables.
For backwards compatiblity, method tables can be generated from old APIs.
2012-05-24 16:31:32 -04:00

32 lines
752 B
Racket

#lang racket
(require generics)
(define-generics (echoable prop:echo echo? #:coerce-method-table list->vector)
(echo echoable))
(struct echo1 (s)
#:property prop:echo
;; defined the "new" way
(methods echoable (define (echo x) (echo1-s x))))
(struct echo2 (s)
#:property prop:echo
;; defined the "old" way
(list (lambda (x) (echo2-s x))))
(struct echo3 (s)
#:property prop:echo
;; happens to get a valid method table, we're good
(vector (lambda (x) (echo3-s x))))
(module+ test
(require rackunit)
(define e1 (echo1 "a"))
(check-equal? (echo e1) "a")
(define e2 (echo2 "b"))
(check-equal? (echo e2) "b")
(define e3 (echo3 "c"))
(check-equal? (echo e3) "c"))