Last active
September 18, 2018 18:08
-
-
Save mofas/64462297757f44d590a0d6bcd564a075 to your computer and use it in GitHub Desktop.
struct/con
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
(require (for-syntax syntax/parse)) | |
(define-syntax (struct/con stx) | |
(syntax-parse stx | |
[(_ id ((pa colon checkers) ... )) | |
#:when (and (map (lambda (x) (eqv? (syntax-e #'x) ':)) (syntax->datum #'(colon ...)))) | |
(begin | |
(define make-id (string->symbol (string-append "make-" (symbol->string (syntax->datum #'id))))) | |
#`(begin | |
(define-struct id (pa ...)) | |
(define old-make #,make-id) | |
(define (check-fn p checker) (unless (checker p) (error 'arguments "~a is not a valid parameter for \"~a\" struct" p (syntax-e #'id)))) | |
(set! #,make-id | |
(lambda (pa ...) | |
(begin | |
((lambda (pa ...) | |
(begin (check-fn pa checkers) ...)) pa ...) | |
(old-make pa ...) | |
)))))])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment