|
@@ -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.
|