English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2009-10-09 (06:55)
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:
Though this won't clearly tell you "why", and I sadly don't know the  

 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.


> 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.