From The Typed Racket Guide, to define a Union Type, just use (define-type Some-Type (U Type1 Type2))
.
To define Polymorphic Data Structures, use something like (define-type (Opt a) (U ...))
.
I want to define a polymorphic binary tree
(define-type (Tree a) (U (Leaf a) Node))
(struct (a) Leaf ([val : a]))
(struct Node ([left : Tree] [right : Tree]))
(define t1 (Leaf 5))
(define t2 (Leaf 8))
(define t3 (Node t1 t2))
I was wondering why the type of t1 is Leaf
not Tree
, and how to make it be a Tree
?
> t1
- : (Leaf Positive-Byte)
#<Leaf>