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

From: Danny Yoo (dyoo at hashcollision.org)
Date: Wed Aug 8 12:28:37 EDT 2012

> 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:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket

(define-values (prop:poke pokeable? pokeable-procedure)
  (make-struct-type-property 'poke))

(define (poke! thing)
  (cond [(pokeable? thing)
         ((pokeable-procedure thing) thing)]
        [else
         (error 'poke! "Don't know how to poke ~e" thing)]))


(struct mascot ()
  #:property prop:poke (lambda (self)
                         (printf "Ho ho!\n")))

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


(struct cartoon-character (name)
    #:property prop:poke (lambda (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.

Posted on the users mailing list.