caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* type inference problem with Printf.sprintf ?
@ 2010-10-26 15:55 Emmanuel Dieul
  2010-10-26 16:04 ` [Caml-list] " Mathias Kende
  2010-10-26 21:41 ` [Caml-list] " David Allsopp
  0 siblings, 2 replies; 10+ messages in thread
From: Emmanuel Dieul @ 2010-10-26 15:55 UTC (permalink / raw)
  To: caml-list

Hi everyone.

I've just noticed a type inference problem (or am I wrong ?). Here's my example :

let format_date =
   let format_valeur = function valeur ->
     (if valeur < 10 then "0" else "") ^ (string_of_int valeur)
   in
   function date ->
     Printf.sprintf "%s/%s/%l %s:%s:%s"
       (format_valeur(date.Unix.tm_mday) : string)
       (format_valeur(date.Unix.tm_mon + 1) : string)
       (date.Unix.tm_year + 1900)
       (format_valeur(date.Unix.tm_hour) : string)
       (format_valeur(date.Unix.tm_min) : string)
       (format_valeur(date.Unix.tm_sec) : string)
;;

The "format_valeur" function has type "int -> string".
The "format_date" function has type "Unix.tm -> string".
The problem is about the Printf.sprintf function.

In fact, if I don't force the type of every date field (like 
"(format_valeur(date.Unix.tm_mday) : string)"),
the ocaml 3.12.0 compiler detects a type error.
This example

let format_date =
   let format_valeur = function valeur ->
     (if valeur < 10 then "0" else "") ^ (string_of_int valeur)
   in
   function date ->
     Printf.sprintf "%s/%s/%l %s:%s:%s"
       format_valeur(date.Unix.tm_mday)
       (format_valeur(date.Unix.tm_mon + 1) : string)
       (date.Unix.tm_year + 1900)
       (format_valeur(date.Unix.tm_hour) : string)
       (format_valeur(date.Unix.tm_min) : string)
       (format_valeur(date.Unix.tm_sec) : string)
;;

generates this error around the "%s/%s/%l %s:%s:%s" string :
Error: This expression has type
          (string -> string -> int -> string -> string -> string -> 'a -> 'b,
           unit, string, string, string, 'a -> 'b)
          format6
        but an expression was expected of type
          (string -> string -> int -> string -> string -> string -> 'a -> 'b,
           unit, string)
          format =
            (string -> string -> int -> string -> string -> string -> 'a -> 'b,
             unit, string, string, string, string)
            format6

Did I do something wrong or is there a real problem ?

Emmanuel


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-11-03 16:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-26 15:55 type inference problem with Printf.sprintf ? Emmanuel Dieul
2010-10-26 16:04 ` [Caml-list] " Mathias Kende
2010-10-26 16:10   ` Emmanuel Dieul
2010-10-26 21:44     ` Arlen Cuss
2010-10-27 11:30       ` Richard Jones
2010-11-03 13:47         ` Gregory Bellier
2010-11-03 14:43           ` Sylvain Le Gall
2010-11-03 15:33             ` [Caml-list] " Benedikt Grundmann
2010-11-03 16:23               ` Dario Teixeira
2010-10-26 21:41 ` [Caml-list] " David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).