Last active
November 2, 2019 23:33
-
-
Save chelseatroy/c000098b09da90c2b0aee886f84120cb to your computer and use it in GitHub Desktop.
Types as Dispatch Procedures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-bob-box x y width height) | |
(define (dispatch message) | |
(cond ((eq? message 'width) width) | |
((eq? message 'height) height) | |
((eq? message 'type) 'bob-box) | |
(else (error "Bad method")))) | |
dispatch) | |
(define (make-alice-box x1 y1 x2 y2) | |
(define (dispatch message) | |
(cond ((eq? message 'width) (abs(- x2 x1))) | |
((eq? message 'height) (abs(- y2 y1))) | |
((eq? message 'type) 'alice-box) | |
(else (error "Bad method")))) | |
dispatch) | |
(define a (make-alice-box 1 2 3 4)) | |
(define b (make-bob-box 1 2 3 4)) | |
(b 'width) ;---> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment