[plt-scheme] Use and abuse of contracts

From: Gordon Weakliem (gweakliem at oddpost.com)
Date: Tue Sep 28 13:32:57 EDT 2004

I saw a presentation last night that touched on the use of contracts in PLT Scheme.  It occurred to me that you could use a contract to do some AOP - like things, for example, to log method calls or to access checking on a method (where only designated callers can call a method).  For example, here's a definition of a silly function with a contract that prints the value of its argument: 
    ; fact is a function taking a number >= 0 and returning a number >= 0 
    fact (-> (and/c (>=/c 1) 
                    (lambda (x) 
                      (printf "Fact called with ~A" 10))) 
                    (>=/c 1)) 
    (lambda (n) 
      (let loop ((i n) 
                 (r 1)) 
        (if (= i 0) 
            (loop (- i 1) (* r i)))))) 
This seems like a potentially useful feature of contracts, but I'm wondering if this amounts to abuse of a contract, or if this is within the valid uses of this feature.  In production code that I've worked on, there's generally a way to switch on logging of things like parameter values, return types, SQL statements to be executed, etc.  It's nice to have a way to have that functionality without cluttering up the code with logging statements. 
Gordon Weakliem 

Posted on the users mailing list.