Created
October 27, 2016 13:15
-
-
Save SuzanneSoy/6449ffefe62b53448f94884cf0c36933 to your computer and use it in GitHub Desktop.
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-syntax (define-enum stx) | |
(syntax-case stx () | |
[(_ case-name contract-name id ...) | |
(with-syntax ([(code ...) (generate-temporaries #'(id ...))]) | |
#'(begin (define-syntax case-name | |
(syntax-rules (id ...) | |
[(_ val [(id) code] ...) | |
(case val | |
[(id) code] | |
...)])) | |
(define contract-name (or/c 'id ...))))])) | |
(define-enum case/foo foo/c a b) | |
(define-enum case/bar bar/c a b c) | |
(define/contract (f-foo v) | |
(-> foo/c number?) | |
(case/foo v | |
[(a) 1] | |
[(b) 2])) | |
(define/contract (f-bar v) | |
(-> bar/c number?) | |
(case/bar v | |
[(a) 10] | |
[(b) 20] | |
[(c) 30])) | |
(f-foo 'a) | |
(f-bar 'c) | |
;; throws error, as expected | |
(f-foo 'c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment