Skip to content

Instantly share code, notes, and snippets.

@corruptmemory
Created December 1, 2011 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corruptmemory/1418666 to your computer and use it in GitHub Desktop.
Save corruptmemory/1418666 to your computer and use it in GitHub Desktop.
define-type is just a type alias...
#lang typed/racket
(define-type A (U String Integer))
(define-type B (U Integer))
(define-type D (U String Real Integer))
(: foo (A -> Integer))
(define (foo a)
(cond [(string? a) (string-length a)]
[(integer? a) a]))
(: bar (B -> Integer))
(define (bar a)
(cond [(integer? a) a]))
(: baz (D -> Integer))
(define (baz a)
(cond [(string? a) (string-length a)]
[(integer? a) (numerator (inexact->exact (truncate a)))]
[(real? a) (numerator (inexact->exact (truncate a)))]))
(: bad (A -> Integer))
(define (bad a) (baz a))
(foo "hello") ;; -> 5
(foo 10) ;; -> 10
(bar 10) ;; -> 10
(bad "444") ;; -> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment