Version française
Home     About     Download     Resources     Contact us    
Browse thread
Why type inference fails in this code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Vincent Aravantinos <vincent.aravantinos@g...>
Subject: Re: [Caml-list] Why type inference fails in this code

Le 9 oct. 09 à 07:25, rouanvd@softwarerealisations.com a écrit :

> ======================================================
> type t = MyInt of int | MyFloat of float | MyString of string ;;
>
> let foo printerf = function
>  | MyInt i -> printerf string_of_int i
>  | MyFloat x -> printerf string_of_float x
>  | MyString s -> printerf (fun x -> x) s
> ;;
> ======================================================


> I read that there are lots of workarounds for the above code  
> snippet, but
> I would like to
> Know why this code fails and what is the best workaround for this  
> type of
> code.

You might find some answers here:
http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#arguments_polymorphes
Though this won't clearly tell you "why", and I sadly don't know the  
answer.

 From my experience the type system limitations are not so big  
hindrance in practice,
you may have to work a bit to get what you want fit in the type system,
but I've never found a thing that wouldn't work at all.

BTW:

> I have done the same thing in Haskell and it correctly infers the  
> type of
> foo.

It doesn't work for me with ghc 6.10.3.
I mean, unless you provided the Haskell compiler with some type  
annotations the same error occurs.
But I am not a haskell expert so I may be wrong.

V.