Version française
Home     About     Download     Resources     Contact us    
Browse thread
question about polymorphic variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: vanicat@d...
Subject: Re: question about polymorphic variants
"Jacques Le Normand" <rathereasy@gmail.com> writes:

> Hello caml-list,
> when I try to run the following code:
>
>
> type sometype =
>   ([
>     `Foo of int
>   | `Bar of int
>   | `Var of 'a ref
>   ] as 'a)
> type someother =
>   ([
>     `Foo of int
>   | `Var of 'a ref
>   ] as 'a)
>
> let foo : someother = `Foo 5;;
> (foo :> sometype)
>
[...]
>
> how do I get around this?

this can't be done: ref type is neither co-variant or
contra-variant :

Imagine:

let foo : someother = `Var (ref Foo 10);;
let bar = (foo :> sometype);;
match bar with
| Var of r -> r:=`Bar 5;;
let baz = match foo with
| Var of r -> x

then baz will be of type someother, with the `Bar 5 value : this can't
be. So you cannot coerce a ref, and so you cannot coerce any value of
a type that include a ref.
-- 
Rémi Vanicat