-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.rkt
28 lines (24 loc) · 994 Bytes
/
misc.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#lang racket/base
(require (for-syntax racket/base
syntax/parse))
(provide (all-defined-out))
;; if `check` is a s-expr and it does not evaluate to a procedure of arity 1,
;; then it is considered an expression evaluating to the value of the test.
;; Otherwise, it is considered a procedure of arity 1 that
;; must be applied to `arg` to obtain the value of the test (this includes most
;; contrats).
(define-syntax (check-argument stx)
(syntax-parse stx
#:context stx
#:track-literals
[(_ arg:id check:id)
#'(unless (check arg)
(raise-argument-error 'arg (format "~a" 'check) arg))]
[(_ arg:id check)
#'(let ([check-val check])
(if (and (procedure? check-val)
(procedure-arity-includes? check-val 1))
(unless (check-val arg)
(raise-argument-error 'arg (format "~a" 'check) arg))
(unless check-val
(raise-argument-error 'arg (format "~a" 'check) arg))))]))