[racket] 'Adult' Racket? (was rackunit vs test-engine)

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Wed Aug 8 15:18:22 EDT 2012

At Wed, 8 Aug 2012 12:28:37 -0400,
Danny Yoo wrote:
> > I wish that I had known about rackunit earlier.  What other
> > 'adult' features of Racket should people would you recommend to
> > readers of HtDP?
> When you think of objects in the Java or C++ sense, there are two
> concepts there that are mixed together: interfaces and inheritance.
> Racket's structure properties gets us interfaces, and they are
> definitely not taught in an intro class.  If we want to have a general
> interface to poke a value, we can do so:

Using `racket/generic' (new in 5.3!):

#lang racket

(require racket/generic)

(define-generics pokeable
  [poke! pokeable])

(struct mascot ()
  #:methods gen:pokeable
  [(define (poke! self) (printf "Ho ho!\n"))])

(define pillsbury-dough-boy (mascot))
(poke! pillsbury-dough-boy)

(struct cartoon-character (name)
  #:methods gen:pokeable
  [(define (poke! self)
     (printf "~a: that tickles!\n" (cartoon-character-name self)))])

(define homer (cartoon-character "Homer"))
(poke! homer)

> There are a few structure property types that are built-into Racket,
> to customize the way that values print as strings, to make a structure
> look like a function, etc.

Racket now provides generic interfaces for some of these. Search for
`gen:' (the conventional prefix for generic interfaces) in the docs.


Posted on the users mailing list.