> On Mar 25, 2019, at 12:30 PM, James Platt <j...@biomantica.com> wrote:
>
>
> On Mar 25, 2019, at 12:05 PM, Matthias Felleisen wrote:
>
>> Your exception handlers may test a contract failure to any level. You can
>> specify this in the predicate part of with-handlers or via match on the exn
>> within the handler function. Regexp matching works well here.
>
> It's obvious enough how to use regex matching for this purpose. My concern
> is partly that it ends up being a lot of code to write and partly that I was
> assuming that the exact wording of error message details could change with
> updates. Maybe these concerns are not so bad as I anticipate.
>
> From The Racket Guide, I did not get the impression that you could test a
> contract at any level. Specifically, I don't see how you tell which
> parameter caused the error. It could just be me not understanding something
> that is actually obvious in the docs or maybe the docs need some work. As
> usual, examples would help. I did search the web for examples of Racket code
> including "exn:fail:contract" and found very little.
Welcome to Racket v7.2.0.10.
> (module a racket (provide (contract-out (f (-> (-> integer? boolean? string?)
> string?)))) (define (f g) (g 1 "not a bool")))
> (require 'a)
> (f (lambda (x y) "hello world"))
; f: broke its own contract
; promised: boolean?
; produced: "not a bool"
; in: the 2nd argument of
; the 1st argument of
; (-> (-> integer? boolean? string?) string?)
; contract from: a
; blaming: a
; (assuming the contract is correct)
; at: stdin:2.41
; [,bt for context]
See how precise the exn message is: 2nd arg of 1st arg of f ~~ not a boolean?
And best of all you can get at it programmatically via exn-message:
> (with-handlers ((exn:fail:contract? exn-message)) (f (lambda (x y) "hello
> world")))
"f: broke its own contract\n promised: boolean?\n produced: \"not a bool\"\n
in: the 2nd argument of\n the 1st argument of\n (-> (-> integer?
boolean? string?) string?)\n contract from: a\n blaming: a\n (assuming the
contract is correct)\n at: stdin:2.41"
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.