Step 7c complete: protocols (define-protocol, implement, satisfies?)
Trait-like dispatch system for record types:
(define-record-type <point>
(make-point x y) point? (x point-x) (y point-y))
(define-protocol Displayable (show self))
(implement Displayable <point>
(show self (str (point-x self) "," (point-y self))))
(show (make-point 3 4)) ;; => "3,4"
(satisfies? "Displayable" (make-point 1 2)) ;; => true
(satisfies? "Displayable" 42) ;; => false
Implementation:
- *protocol-registry* global dict stores protocol specs + implementations
- define-protocol creates dispatch functions via eval-expr (dynamic lambdas)
- implement registers method lambdas keyed by record type name
- Dispatch: (type-of self) → lookup in protocol impls → call method
- satisfies? checks if a record type has implementations for a protocol
2645 tests pass (+1 from protocol self-test).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user