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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris King <colanderman@g...>
Subject: Re: Polymorphic variants question
On 9/1/06, David Allsopp <dra-news@metastack.com> wrote:
> Now, if I try to constrain it to what I'm after with
> 
> let (f : [`A | `C] -> bool * [`A | `B | `C]) = fun x -> ...
> 
> then I get a type error unless I change
> 	(false, x)
> to
> 	(false, id x)
> with 
> 	let id = function `A -> `A | `C -> `C
> 
> Is there a better way of writing this?

Yes, you can use the coercion operator to tell the compiler to expand the type of x to include `B:

# let f (x: [`A | `C]) = if x = `A then (true, `B) else (false, (x :> [`A | `B | `C]))
val f : [ `A | `C ] -> bool * [ `A | `B | `C ] = <fun>

Perhaps someone else can clarify how this accomplishes the same thing as your id function, as my understanding of type theory is somewhat rudimentary.

- Chris King