Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active November 2, 2019 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/11e7acda1e60443d6333c9159701f6a3 to your computer and use it in GitHub Desktop.
Save chelseatroy/11e7acda1e60443d6333c9159701f6a3 to your computer and use it in GitHub Desktop.
Type Registry
#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