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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] Re: records with polymorphic variants?
On Sun, 4 Mar 2007, Lukasz Stafiniak wrote:

> On 3/4/07, Zheng Li <li@pps.jussieu.fr> wrote:
>> 
>> Hi,
>> 
>> Eliot Handelman <eliot@generation.net> writes:
>> > type f = [ `A of int ]
>> >
>> > type r = { x : f }
>> >
>> > but then I can't do this:
>> >
>> > { x = `B "test" }
>> Here you want type f polymorphic, however as the type of a record field, 
>> its
>> polymorphism has to be reflected (bound) as type parameter in the 
>> declaration
>> of r.
>
> Yes, but the type f itself is not polymorphic, it is not [> f].

You can do this directly if you prefer:

type 'a f = 'a
   constraint 'a = [> `A of int ]

>> I guess you want the follows
>> 
>> # type 'a r = { x : 'a } constraint 'a = [> f]
>> type 'a r = { x : 'a; } constraint 'a = [> f ]
>> # {x = "test"}
>> Characters 6-12:
>>   { x = "test"};;
>>         ^^^^^^
>> This expression has type string but is here used with type [> f ]
>> # {x = `B "test"}
>> - : [> `A of int | `B of string ] r = {x = `B "test"}
>> 
> Nice! I didn't realize this is possible. Thanks.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

--
Martin Jambon
http://martin.jambon.free.fr