Changeset 38817 in project


Ignore:
Timestamp:
08/06/20 18:11:51 (4 months ago)
Author:
juergen
Message:

generics 2.0.1 with docu fixed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/generics

    r38615 r38817  
    314314with corresponding selectors and stepping down the tree in case the
    315315first selector succeeds. So we reach eventually a matching method.
    316 #########
     316
    317317The two fundamental macros are define-generic and define-method. The
    318318former creates a closure with state a one-item method-tree, which can be
     
    327327follows:
    328328
    329   (define-generic (Name (x x??) ....) body ....)
    330   (define-generic (Name (x x??) ... xs xs??) body ....)
    331 
    332 for fixed or variable argument lists respectively and -- with the same
    333 syntax
     329  (define-generic (Name x ....) body ....)
     330  (define-generic (Name x ... . xs) body ....)
     331
     332for fixed or variable argument lists respectively and -- with selectors
    334333
    335334  (define-method (Name (x x??) ....) body ....)
     
    369368consider the following definitions
    370369
    371   (define-generic (Add (x number??) (y number??)) (+ x y))
     370  (define-generic (Add x y) (error 'Add "no method found")
     371  (define-method  (Add (x number??) (y number??)) (+ x y))
    372372  (define-method  (Add (x fixnum??) (y fixnum??)) (fx+ x y))
    373373
     
    384384complicates matters.
    385385
    386 === The module generics
    387 
    388 ==== generics
    389 
    390 <procedure>(generics sym ..)</procedure>
     386==== generic-functions
     387
     388<procedure>(generic-functions sym ..)</procedure>
    391389
    392390documentation procedure
     
    394392==== define-generic
    395393
    396 <macro>(define-generic (Name (x x??) ....) body ....)</macro>
    397 <macro>(define-generic (Name (x x??) ... xs xs??) body ....)</macro>
     394<macro>(define-generic (Name x ....) body ....)</macro>
     395<macro>(define-generic (Name x ... . xs) body ....)</macro>
    398396
    399397defines a new generic function Name with one anonymous
     
    644642;; non-variadic generics
    645643;; ---------------------
    646 (define-generic (Add (x number??) (y number??)) (+ x y))
    647 (define-method  (Add (x fixnum??) (y fixnum??)) (fx+ x y))
     644(define-generic (Add x y) (error 'Add "no method defined"))
     645(define-method (Add (x number??) (y number??)) (+ x y))
     646(define-method (Add (x fixnum??) (y fixnum??)) (fx+ x y))
    648647
    649648(generic? Add) ; -> #t
     
    656655;; sequences
    657656;; ---------
    658 (define-generic (At (k index??) (seq list??)) (list-ref seq k))
    659 (define-generic (Drop (k index??) (seq list??)) (list-tail seq k))
    660 (define-generic (Take (k index??) (seq list??))
     657(define-generic (At k seq) (error "At no method defined"))
     658(define-method (At (k index??) (seq list??)) (list-ref seq k))
     659(define-generic (Drop k seq) (error 'Drop "no method define"))
     660(define-method (Drop (k index??) (seq list??)) (list-tail seq k))
     661(define-generic (Take k seq) (error 'Take "no method defined"))
     662(define-method (Take (k index??) (seq list??))
    661663                (let loop ((n 0) (lst seq) (result '()))
    662664                  (if (fx= n k)
     
    687689;; variadic generics
    688690;; -----------------
    689 (define-generic (Add* xs number??) (apply + xs))
     691(define-generic (Add* . xs) (error 'Add* "no method defined"))
     692(define-method (Add* xs number??) (apply + xs))
    690693(define-method (Add* xs list??) (apply append xs))
    691694(Add* 1 2 3) ; -> 6
     
    702705== Last update
    703706
    704 Apr 15, 2020
     707Aug 06, 2020
    705708
    706709== Author
     
    740743
    741744== Version History
     745; 2.0.1 : inline and online documentation fixed
    742746; 2.0 : egg restructured, one module renamed, generic-helpers enhanced
    743747; 1.0 : ported from chicken-4
Note: See TracChangeset for help on using the changeset viewer.