Last active
November 2, 2019 23:47
-
-
Save chelseatroy/11e7acda1e60443d6333c9159701f6a3 to your computer and use it in GitHub Desktop.
Type Registry
This file contains 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
#lang racket | |
(define (register h method tag function) | |
(hash-set! h (list tag method) function)) | |
(define (lookup h key) | |
(hash-ref h key)) | |
(define h (make-hash)) | |
(register h 'alice-box 'width alice-width) | |
(register h 'bob-box 'width bob-width) | |
(define (width box) | |
((lookup h (list 'width (car box))) box)) | |
(define (height box) | |
((lookup h (list 'height (car box))) box)) | |
(define b (make-bob-box 1 2 3 4)) | |
(width b) ;--> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment