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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Serge Aleynikov <serge@h...>
Subject: Re: [Caml-list] parameterized pattern
Jon Harrop wrote:
> On Monday 06 November 2006 21:15, Serge Aleynikov wrote:
>   
>> Could anyone point at a suitable resource?
>>     
>
> I wasn't even aware that you could add type annotations inside a pattern. 
I had a similar reaction when I saw that construct in the formal 
language spec.
> Your 
> example will not work simply because OCaml's type system will not allow float 
> to unify with string. You need some context where type inference will allow 
> different possibilities, like a tuple containing both a float and a string:
>
> # let f = function
>   | (y : float), _ -> print_float y
>   | _, (s : string) -> print_string s;;
>
> or a polymorphic variant containing either a float or a string:
>
> # let f = function
>   | `F (y : float) -> print_float y
>   | `S (s : string) -> print_string s;;
> val f : [< `F of float | `S of string ] -> unit = <fun>
>   
Thanks.  Several people also replied clarifying that the type 
annotations were merely used for aiding the type inferencer.

Serge

-- 
Serge Aleynikov
Routing R&D, IDT Telecom
Tel: +1 (973) 438-3436
Fax: +1 (973) 438-1464