[racket] Typed Racket struct with more permissive constructor

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Thu May 31 11:50:04 EDT 2012

On Thu, May 31, 2012 at 11:42 AM, Neil Toronto <neil.toronto at gmail.com> wrote:
> Currently, the floating-point bitmap type is defined so that its size fields
> are Integer, and a guard assures that they're really Nonnegative-Fixnum:
>
> (struct: flomap ([values : FlVector]
>                 [components : Integer]
>                 [width : Integer]
>                 [height : Integer])
>  #:transparent
>  #:guard
>  (λ (vs c w h name)
>    (with-asserts ([c  nonnegative-fixnum?]
>                   [w  nonnegative-fixnum?]
>                   [h  nonnegative-fixnum?])
>      [...] ; elided length check on vs
>      (values vs c w h))))
>
>
> What I'd *really* like, though, is to have the fields be Nonnegative-Fixnum
> in the first place, and simply have a more permissive constructor. Is there
> a way to do that? As it is, `flomap-components' always returns an Integer,
> but no flomap instance can actually have an integer-valued `components'
> field.

Could you just structure the program this way?

(struct: flomap ([values : FlVector]
                [components : Nonnegative-Fixnum]
                [width : Integer]
                [height : Integer])
 #:transparent)

(: make-flomap : FlVector Integer Integer Integer -> flomap)
(define (make-flomap v c w h)
  (with-asserts ... (flomap v c w h)))
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.