diff --git a/unstable/lens/if.rkt b/unstable/lens/if.rkt index ce1fab7..7bd2090 100644 --- a/unstable/lens/if.rkt +++ b/unstable/lens/if.rkt @@ -38,11 +38,16 @@ (λ (tgt) (cond [(pred tgt) (lens-view lens tgt)] ... - [else (error 'lens-cond "expected ~a, given: ~v" '(or/c pred-expr ...) tgt)])) + [else (raise-lens-cond-error tgt 'pred-expr ...)])) (λ (tgt nvw) (cond [(pred tgt) (lens-set lens tgt nvw)] ... - [else (error 'lens-cond "expected ~a, given: ~v" '(or/c pred-expr ...) tgt)]))))])) + [else (raise-lens-cond-error tgt 'pred-expr ...)]))))])) + +(define (raise-lens-cond-error tgt . pred-expr-syms) + (raise-arguments-error 'lens-cond "no matching clause for target" + "target" tgt + "expected" `(or/c ,@pred-expr-syms))) (define-syntax lens-match (syntax-parser diff --git a/unstable/lens/if.scrbl b/unstable/lens/if.scrbl index e9b391d..a2d8986 100644 --- a/unstable/lens/if.scrbl +++ b/unstable/lens/if.scrbl @@ -26,7 +26,8 @@ Creates a lens that uses @racket[lens1] when the target satisfies (lens-cond [pred-expr lens-expr] ...)]]{ Like @racket[lens-if], but based on @racket[cond] instead of @racket[if]. It creates a lens that uses the first lens if the target matches the first -predicate, the second lens if the target matches the second predicate, and so on. +predicate, the second lens if the target matches the second predicate, and so on. If the +target matches none of the predicates, an error is raised. @lens-unstable-examples[ (define cond-lens (lens-cond [list? first-lens] [vector? (vector-ref-lens 0)] @@ -37,6 +38,7 @@ predicate, the second lens if the target matches the second predicate, and so on (lens-set cond-lens '(1 2 3) 'a) (lens-set cond-lens '#(1 2 3) 'a) (lens-set cond-lens "123" #\a) + (lens-view cond-lens 'none-of-the-above) ]} @defform[(lens-match [pat lens-expr] ...)]{