[plt-scheme] Question on ill-defined contract
On Tue, 5 Jun 2007, Paulo J. Matos wrote:
> Then since the only interesting parameter was a, b was by default 0
> and c was by default a + 5, I changed it to a opt-lambda:
> (define foo-fn
> (opt-lambda (a (b 0) (c (+ a 5)))
> (+ a b c)))
>
> But I forgot to update the contract, when I went to show everyone from
> module bar, how opt-lambda would work nicely, I have:
> (module bar mzscheme
> (require "foo.scm")
> (foo-fn 5))
>
> and I get:
> procedure foo-fn: expects 3 arguments, given 1: 5
I don't think this is broken; the above is a contract error! We see the
same issue with:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module foo mzscheme
(require (lib "contract.ss"))
(provide/contract [f (number? . -> . number?)])
(define (f x)
(* x x)))
(module bar mzscheme
(require "foo.scm")
(f 3 4))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Here, we also see a similar error message:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
procedure f: expects 1 argument, given 2: 3 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
But this is a contract-level error: we can see this by wrapping a handler:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module bar mzscheme
(require "foo.scm")
(with-handlers ((exn:fail:contract?
(lambda (exn) (printf "contract error!~n"))))
(f 3 4)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
After which we see the "contact error!" message. (Specifically, the
exception raised is exn:fail:contract:arity).
Maybe you're asking for the default error display handler or the
exception's exn-message to make it clear that a contract error? is going
on?
Best of wishes!