[plt-scheme] Questions about contracts

From: Noel Welsh (noelwelsh at yahoo.com)
Date: Mon Apr 16 08:14:49 EDT 2007

It would be useful to be able to add optional messages to contracts, like you can do with checks in SchemeUnit.  It would also be useful to have an API for destructuring contracts.  If these two features we added then a fair amount of code I deal with would disappear!  I'm not just talking about SchemeUnit; in our web apps we write lots of validation code that essentially duplicates what contracts do (with these additional features).  E.g. to check a student record is valid:

(list (annotate-fields '(username)
              (cond [(not username)
                     (fail "Value is required.")]
                    [(> (string-length username) 8)
                     (fail "Value is too long (max length 8 characters).")]
                    [(not (pregexp-match username-regexp username)) 
                     (fail "Value is not a 4 to 8 character lowercase alphanumeric code.")]
                    [(and strict?
                          (find-one* (s:alias 'a entity:student)
                                     (s:where (s:and (if id (s:<> (s:ref 'a 'id) id) #t)
                                                     (s:= (s:ref 'a 'username) username)))))
                     (fail (format "Student already exists with username \"~a\"." username))]
                    [else (pass)]))
            (annotate-fields '(number)
              (cond [(not number) 
                     (fail "Value is required.")]
                    [(> (string-length number) 9)
                     (fail "Value is too long (max length 9 characters).")]
                    [(not (pregexp-match student-number-regexp number))
                     (fail "Value is not a 9 digit number.")]

There are probably hundreds of lines of this sort of stuff.  It is important to decouple to definition of the contract from the application, as we apply contracts in various different ways (sometimes we generate warnings, not errors, something we are less strict in our checks) but I think the contract library already does this.


----- Original Message ----
From: Robby Findler <robby at cs.uchicago.edu>
To: Henk Boom <lunarc.lists at gmail.com>
Cc: plt-scheme at list.cs.brown.edu
Sent: Monday, April 16, 2007 12:36:37 PM
Subject: Re: [plt-scheme] Questions about contracts

The contract system currently doesn't go inside and/c contracts when
printing out the specific part of the contract that failed. This is
both good and bad. In your case, you'd like a little less context, but
if you use contracts like 'integer?' it can sometimes be convenient to
have a little more context in the contract error message.

I've considered adding some kind of path information to the error
messages, but that adds additional complexity too, so haven't
attempted it yet.


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

Posted on the users mailing list.