> You could use a union type, but I’m not sure if a large union type would
work well in terms of performance?

I don't think this will work.

This takes a long time to run (although I'm not sure how much of this is
the macro expansion).

Anyway, if it takes this long on a union type with 10000 things in it, I
highly doubt it can support a union type with over a million.

#lang typed/racket

(require (for-syntax syntax/parse
                     racket/list))

(define-syntax (define-range stx)
  (syntax-parse stx
    [(_ name range-low range-hi)
     #`(define-type name (U #,@(range (syntax-e #'range-low)
                                      (syntax-e #'range-hi))))]))

(define-range hello 0 10000)


~Leif Andersen

On Tue, Apr 21, 2015 at 10:41 AM, Matthias Felleisen <[email protected]>
wrote:

> #lang typed/racket
>
> (define-type NDigit (U 0 1 2 3 4 5 6 7 8 9))
>
> (define-type SDigit (U 'zero 'one 'two 'three 'four 'five 'six 'seven
> 'eight 'nine))
>
> (: to-string (-> NDigit SDigit))
> (define (to-string i)
>   (case i
>     [(0) (displayln i) 'zero]
>     [(1) (displayln i) 'zero]
>     [(2) (displayln i) 'zero]
>     [(3) (displayln i) 'zero]
>     [(4) (displayln i) 'zero]
>     [(5) (displayln i) 'zero]
>     [(6) (displayln i) 'zero]
>     [(7) (displayln i) 'zero]
>     [(8) (displayln i) 'zero]
>     [(9) (displayln i) 'zero]
>     ;; can't get here
>     [else (displayln (+ i i)) 'one]))
>
>
> I wish mousing over i would give me more precise types here. Occurrence
> typing subtracts 2 ... from i's type but somehow it doesn't work thru case.
>
>
> On Apr 20, 2015, at 9:09 PM, Benjamin Greenman wrote:
>
> The contract integer-in lets me guarantee an integer is within a certain
> range.
>
> (define/contract (mod3 n)
>   (-> integer? (integer-in 0 2))
>   (modulo n 3))
>
> Is there a similar way to specify a type for an integer range? Or do I
> need to use a union type? (I'd really like to specify a range containing
> over a million integers.)
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to