Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] polymorphic variant question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] polymorphic variant question
nadji@noos.fr writes:

> Hi all,
>
> I am puzzled by the code below which doesn't work.
>
> # let f = function `B -> ();;
> val f : [ `B] -> unit = <fun>
> # let g x = match (x:[`A|`B]) with `A -> () | y -> f y;;
> Characters 51-52:
>   let g x = match (x:[`A|`B]) with `A -> () | y -> f y;;
>                                                      ^
> This expression has type [ `A | `B] but is here used with type [ `B]
> (denoting the "y" from the "f y")
>
> How can I say to OCaml that "y" can't be of type `A ?

well you can do the following :

let g x = match (x:[`A|`B]) with `A -> () | `B as y -> f y;;

for more complex case, you can do something like :

type b = [ `B ]

let g x = match (x:[`A|`B]) with `A -> () | #b as y -> f y;;

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners