shriram / plai-public

Repository for (for now) filing bug reports about PLAI.
6 stars 0 forks source link

use typed/racket/class as an example for structural typing #32

Open bremner opened 6 months ago

bremner commented 6 months ago

Contact Details

bremner@unb.ca

Which part are you commenting on?

p. 166, v3.2.2

What's your suggestion?

I recently learned about typed/racket/class. Although it is apparently still experimental, I believe it can serve as a concrete example of structural typing. Although it is a bit more verbose than the pseudocode in the book, it is nice to see that TR infers the corresponding types, and that it works for node%, even though the type is not precisely equal.

#lang typed/racket
(require typed/racket/class)
(require typed/rackunit)

(define node%
  (class object%
    (init [with-size : Real])
    (define current-size : Real with-size)
    (define/public (size) current-size)
    (super-new)))

(define empty% 
  (class object%
    (define/public (size) 0)
    (super-new)))

(define mt%
  (class object%
    (define/public (size) 0)
    (super-new)))

(define (m [arg : (Object (size (-> Real)))]) : Real
  (send arg size))

(check-equal? (m (new empty%)) 0)
(check-equal? (m (new mt%)) 0)
(check-equal? (m (new node% [with-size 2])) 2)