(module record-case mzscheme
(require (lib "plt-match.ss"))
(require-for-syntax (file "private/check-syntax.ss"))
(provide record-case record-case*)
(define-syntax (record-case stx)
(<record-case> stx) (with-syntax ([(record-case . d) stx])
(syntax (record-case* . d))))
(define-syntax (record-case* stx)
(let ((record-case-clause->match-clause
(lambda (stx)
(syntax-case stx (else)
[(n (x ...) a) #'[(list-rest 'n vals) (match vals
[(list-rest x ... _) a]
[_ (rc-error 'n '(x ...) vals)])]]
[(n (x ... . y) a) #'[(list-rest 'n x ... y) a]]
[(else a) #'[_ a]]))))
(syntax-case stx ()
[(record-case e c ...)
(with-syntax ([(c* ...) (map record-case-clause->match-clause
(syntax->list #'(c ...)))])
#'(match e c* ...))])))
(define (rc-error symbol formals vals)
(error 'record-case "Clause ~a expects ~a values, given ~a, ~a"
(list symbol formals '...)
(length formals)
(length vals)
vals))
)