Browse Source

add overload and update readme

Mistivia 4 months ago
parent
commit
98860c8aca
3 changed files with 50 additions and 9 deletions
  1. 1 1
      LICENSE
  2. 48 8
      README.md
  3. 1 0
      trait.scm

+ 1 - 1
LICENSE

@@ -1,4 +1,4 @@
-Copyright (c) 2024 Mistivia.
+Copyright (c) 2024 Mistivia <i (at) mistivia (dot) com>.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without

+ 48 - 8
README.md

@@ -32,6 +32,12 @@ When calling `<func-name>`, implementations will be selected by the first argume
     
 Sometimes the implementation of a function cannot be decided though the arguments, in such case, `with-type-of` can give you the implementation through specifying the object.
 
+### define-overload
+
+    (define-overload (<func-name> (<param> <pred>)|<param> ...))
+    
+Overload a function by type predicates to make it generic.
+
 ## Examples
 
 ### Eq
@@ -133,7 +139,8 @@ Sometimes the implementation of a function cannot be decided though the argument
       	   (lambda (e2 r2 c2)
       	     (define (transform-func ,@(cdr def))
       	       ,@body)
-      	     (apply transform-func (cdr e2))))))))))
+      	     (apply transform-func (cdr e2)))))))))
+    )
     
     (module Eq (Eq == /=)
       (import scheme
@@ -144,12 +151,13 @@ Sometimes the implementation of a function cannot be decided though the argument
         (==)
         (/= (lambda (a b)
               (display ==) (newline)
-              (not (== a b))))))
+              (not (== a b)))))
+    )
     
     (module point (point?
                    make-point
                    point==?
-                   impl-Eq-point)
+                   impl-point-traits)
       (import scheme
               (chicken base)
               Eq
@@ -166,9 +174,10 @@ Sometimes the implementation of a function cannot be decided though the argument
       
       ;; trait implementations should be defined as macros
       ;; to export to other modules
-      (define-macro (impl-Eq-point)
+      (define-macro (impl-point-traits)
         '(define-trait-impl (Eq point?)
-           (== point==?))))
+           (== point==?)))
+    )
     
     (module mymodule ()
       (import scheme
@@ -178,13 +187,44 @@ Sometimes the implementation of a function cannot be decided though the argument
               point)
       
       ;; import trait implementation
-      (impl-Eq-point)
+      (impl-point-traits)
     
       (display (== (make-point 1 2) (make-point 1 2)))
       (newline)
       (display (/= (make-point 1 2) (make-point 1 2)))
-      (newline))
+      (newline)
+    )
+
+### Multiparam
+
+    (import trait)
+    (import (chicken condition))
+    
+    (define-record point x y)
+    
+    (define (multiply a b)
+      (abort "`multiply` not implemented."))
+    
+    (define-overload (multiply (a number?) (b point?))
+      (make-point (* a (point-x b)) (* a (point-y b))))
+    
+    (define-overload (multiply (a point?) (b number?))
+      (multiply b a))
+    
+    (define-overload (multiply (a number?) (b number?))
+      (* a b))
+    
+    (define-overload (multiply (a point?) (b point?))
+      (+ (* (point-x a) (point-x b))
+         (* (point-y a) (point-y b))))
+    
+    (display (list (multiply 1 2)
+                   (multiply 1 (make-point 3 4))
+                   (multiply (make-point 5 6) 7)
+                   (multiply (make-point 8 9) (make-point 10 11))))
+    (newline)
+
 
 ## License
 
-This library is relaeased by BSD license.
+This library is BSD licensed, see LICENSE for details.

+ 1 - 0
trait.scm

@@ -2,6 +2,7 @@
                define-trait
                define-trait-impl
                with-type-of
+               define-overload
                ;; functions below used only in macros
                get-trait-func
                create-trait)