Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics #58

Merged
merged 12 commits into from
Jul 13, 2023
Merged

Generics #58

merged 12 commits into from
Jul 13, 2023

Conversation

bksaiki
Copy link
Owner

@bksaiki bksaiki commented Jul 13, 2023

This PR adds generic functions. Generic function interfaces are defined by the define-generics syntactic form:

(define-generics <id>
                 [<method-id> . <formals>]
                 ...)

<formals> ::= (<arg> ...)
          ::= (<arg> ... . <rest-id>)
          ::= <rest-id>

The syntactic form binds the identifier gen:<id> to an object that encodes a generics interfaces and the identifier gen:<id>? to a predicate that returns #t if a record type implements the interface. Records implement generic function interfaces via the methods clause. See the definition of set as an example:

(define-record-type set
  (opaque #t)
  (sealed #t)
  (fields store)
  (protocol
    (lambda (p)
      (lambda xs
        (define h (make-hashtable))
        (for-each (lambda (x) (hashtable-set! h x #f)) xs)
        (p h))))
  (methods gen:equal+hash
    (lambda (a b rec-equal?)
      (rec-equal? (set-store a) (set-store b)))
    (lambda (a rec-hash)
      (+ (rec-hash (set-store a)) 401235121)))
  (methods gen:custom-write
    (lambda (s port mode)
      (fprintf port "#<set ~s>" (hashtable-keys (set-store s))))))

where gen:equal+hash and gen:custom-write are interfaces defined by

(define-generics equal+hash
                 [equal-proc a b rec-equal?]
                 [hash-proc a rec-hash])

(define-generics custom-write
                 [write-proc v port mode])

Fixes bugs in the evaluator that causes the garbage collector to GC prematurely.

@bksaiki bksaiki merged commit 3abadc5 into main Jul 13, 2023
@bksaiki bksaiki deleted the generics branch July 13, 2023 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant