README.md 782 B

Trait

A trait/typeclass system for Scheme.

Example

(import trait)

(define-record point x y)

(define-trait Eq
  ;; fallback to equal? by default
  (same? equal?))

(define-trait-impl (Eq number?)
  (same? =))

(define-trait-impl (Eq symbol?)
  (same? eq?))

(define-trait-impl (Eq point?)
  (same? (lambda (a b) (and (point? b)
                            (= (point-x a) (point-x b))
                            (= (point-y a) (point-y b))))))

(display (list (same? 'a 'a)
               (same? 1 1)
               (same? 1 2)
               (same? (list 1 2) (list 1 2))
               (same? (cons 3 4) (cons 3 4))
               (same? (make-point 1 2) (make-point 3 4))))