在Chicken Scheme中模拟typeclass

Mistivia 7870c8f6e6 remove tabs hace 4 meses
LICENSE 98e8c71983 init hace 4 meses
README.md 7870c8f6e6 remove tabs hace 4 meses
release-info 98e8c71983 init hace 4 meses
trait.egg 98e8c71983 init hace 4 meses
trait.scm 7870c8f6e6 remove tabs hace 4 meses

README.md

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))))