[plt-scheme] Use and abuse of contracts

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Sep 29 06:37:27 EDT 2004

On Sep 28, 2004, at 1:32 PM, Gordon Weakliem wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 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:
> (define/contract
>     ; 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)
>             r
>             (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.

Just write a macro or a higher-order function that you can wrap around 
some other contract, like any, and that does everything for you.

It's still an abuse, but if it makes you happy :-) -- Matthias

P.S. Have you tried "show trace"?

> -- 
> Gordon Weakliem
> http://www.eighty-twenty.net

Posted on the users mailing list.