Skip to content

Instantly share code, notes, and snippets.

@mofas
Last active September 18, 2018 18:08
Show Gist options
  • Save mofas/64462297757f44d590a0d6bcd564a075 to your computer and use it in GitHub Desktop.
Save mofas/64462297757f44d590a0d6bcd564a075 to your computer and use it in GitHub Desktop.
struct/con
(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