[racket] help with typed racket function?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Sun Mar 13 20:36:44 EDT 2011

I'm trying to make a function that can discriminate typed boxes: I
tried the following in Typed Racket:

(: ensure-number-box (Any -> (Boxof Number)))
(define (ensure-number-box x)
  (if (and (box? x) (number? (unbox x)))
      x
      (error 'ensure-number-box "~s" x)))

Unfortunately, I can't convince the type checker that x is a (Boxof
number).  I'm not quite sure how to use the occurrence typing properly
to get this to type correctly.


I want to return the original box.  That is, it's not correct for me to do this:

(: ensure-number-box (Any -> (Boxof Number)))
(define (ensure-number-box x)
  (if (box? x)
      (let ([y (unbox x)])
        (if (number? y)
            (box y)
            (error 'ensure-number-box "~s" x)))
      (error 'ensure-number-box "~s" x)))

since this generates a fresh box.


Posted on the users mailing list.