[racket] Type-class-like idiom in Racket?

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Mon Mar 5 09:35:55 EST 2012

At Sun, 4 Mar 2012 20:36:23 -0800,
Helmut Rohrbacher wrote:
> The only thing is, how do you extend these properties to built-in types
> like LIST and VECTOR? I'm guessing that you'd need to wrap them in a
> user-defined type...

You could do that, or you could handle them specially in your generic
function. Using Danny's example:

    (define (doolittle! entity)
      (cond
        [(list? entity)
	 (printf "Lists don't speak.")]
        [(prop:speak? entity)
         (printf "~a says: " entity)
         ((prop:speak-ref entity) entity)]
        [else
         (raise-type-error 'speak! "speaking animal" entity)]))


Vincent

Posted on the users mailing list.