Version française
Home     About     Download     Resources     Contact us    
Browse thread
type inference problem with Printf.sprintf ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Arlen Cuss <celtic@s...>
Subject: Re: [Caml-list] type inference problem with Printf.sprintf ?
> Le 26/10/2010 18:04, Mathias Kende a écrit :
> > Le mardi 26 octobre 2010 à 17:55 +0200, Emmanuel Dieul a écrit :
> >> 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)
> >> ;;

By the way, consider using this:
Printf.sprintf "%02d/%02d/%l %02d:%02d:%02d"
  date.Unix.tm_mday
  (date.Unix.tm_mon + 1)
  (date.Unix.tm_year + 1900)
  date.Unix.tm_hour
  date.Unix.tm_min
  date.Unix.tm_sec;;

.. to make format_valeur redundant. Or, you could even use CalendarLib
to do pretty printing:

# open CalendarLib;;
# Calendar.now ();;
- : CalendarLib.Calendar.t = <abstr>
# Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
26/10/2010 21:43:43
- : unit = ()
# 

> >>
> > Here, the argument to the Printf.sprintf function are :
> > "%s/%s/%l %s:%s:%s", format_valeur, date.Unix.tm_mday, ...
> >
> > Just add parenthesis around (format_valeur date.Unix.tm_mday) (but
> > without any type annotation) to pass the _result_ of this application to
> > the sprintf function. You don't need parenthesis around the argument of
> > the function though.
> >
> > Mathias
> >
>