Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question on Variant Types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Question on Variant Types
> type foo = [`Nil | `Tree of foo]
> type bar = [`Nil | `Leaf of int | `Tree of bar]
>
> let f x : foo = x
> let g x : bar = x
> let a = `Tree (`Nil)
> let b = `Tree (a)
> let c = `Tree (f a)
> let d = `Tree (`Leaf 1)
>
> As is proper, I can run f on a, b, and c, but not on d. D is not a valid
> foo.
> But I cannot run g on c. This makes sense, as I have said 'a tree of bars
> contains a bars.' But I want to somehow note that a tree of bars MIGHT
> contain foo's. Is there any way to annotate this?

>From the ocaml reference docs:

# let bar_of_foo t = (t : foo :> bar);;
val bar_of_foo : foo -> bar = <fun>
# g (bar_of_foo c);;
- : bar = `Tree (`Tree `Nil)

Jonathan