Bladeren bron

add example with modules

Mistivia 4 maanden geleden
bovenliggende
commit
19d0723b1c
1 gewijzigde bestanden met toevoegingen van 76 en 3 verwijderingen
  1. 76 3
      README.md

+ 76 - 3
README.md

@@ -14,6 +14,7 @@ I prefer the name *typeclass*, but there has been an egg named typeclass already
     
     (define-trait Eq
       (==)
+      ;; with a default implementation
       (/= (lambda (a b) (not (== a b)))))
     
     (define-trait-impl (Eq number?)
@@ -78,9 +79,81 @@ I prefer the name *typeclass*, but there has been an egg named typeclass already
     (display (nullable-value (>>= (make-some 3) *2)))
     (newline)
     
-    (display ((with-type nullable? return) 42))
-    (newline)
-
+    ;; For a function in trait, the implementation is selected by
+    ;; applying the predicates to the first argument.
+    ;; When the varaible of related type is the return value or
+    ;; the 2nd/3rd/... argument, use (with-type-of var trait function)
+    ;; to get the right function.
     (define x (make-some 42))
     (let ((return (with-type-of x Monad return)))
       (display (nullable-value (return 99))))
+
+### With Multiple Modules
+
+    (module define-macro (define-macro)
+      ;; import
+      (import scheme
+      	  (chicken syntax))
+      ;; body
+      (define-syntax define-macro
+        (er-macro-transformer
+         (lambda (exp r c)
+           (let ((def (cadr exp))
+      	   (body (cddr exp)))
+             `(define-syntax ,(car def)
+      	  (er-macro-transformer
+      	   (lambda (e2 r2 c2)
+      	     (define (transform-func ,@(cdr def))
+      	       ,@body)
+      	     (apply transform-func (cdr e2))))))))))
+    
+    (module Eq (Eq == /=)
+      (import scheme
+              (chicken base)
+              trait)
+    
+      (define-trait Eq
+        (==)
+        (/= (lambda (a b)
+              (display ==) (newline)
+              (not (== a b))))))
+    
+    (module point (point?
+                   make-point
+                   point==?
+                   impl-Eq-point)
+      (import scheme
+              (chicken base)
+              Eq
+              trait
+              define-macro)
+    
+      (define-record point x y)
+    
+      (define (point==? a b)
+        (and (point? a)
+             (point? b)
+             (= (point-x a) (point-x b))
+             (= (point-y a) (point-y b))))
+      
+      ;; trait implementations should be defined as macros
+      ;; to export to other modules
+      (define-macro (impl-Eq-point)
+        '(define-trait-impl (Eq point?)
+           (== point==?))))
+    
+    (module mymodule ()
+      (import scheme
+              (chicken base)
+              trait
+              Eq
+              point)
+      
+      ;; import trait implementation
+      (impl-Eq-point)
+    
+      (display (== (make-point 1 2) (make-point 1 2)))
+      (newline)
+      (display (/= (make-point 1 2) (make-point 1 2)))
+      (newline))
+