caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* printf "%a" vs sprintf "%a"
@ 2009-03-26 16:53 Tiphaine Turpin
  2009-03-26 17:29 ` [Caml-list] " David Rajchenbach-Teller
  2009-03-26 17:29 ` Jérémie Dimino
  0 siblings, 2 replies; 4+ messages in thread
From: Tiphaine Turpin @ 2009-03-26 16:53 UTC (permalink / raw)
  To: caml-list

Hi,

I'am confused by the interpretation of "%a":

# Printf.printf "%a";;
- : (out_channel -> '_a -> unit) -> '_a -> unit = <fun>
# Printf.sprintf "%a";;
- : (unit -> '_a -> string) -> '_a -> string = <fun>

Usually, the typing of formatting functions is such that

printf something

has type unit if and only if

sprintf something

has type string. But %a breaks this rule. Wouldn't it be simpler to have
two separate directives which accept respectively string printers and
channel printers, regardless of the outer printing function ?

Tiphaine


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

* Re: [Caml-list] printf "%a" vs sprintf "%a"
  2009-03-26 16:53 printf "%a" vs sprintf "%a" Tiphaine Turpin
@ 2009-03-26 17:29 ` David Rajchenbach-Teller
  2009-03-26 17:29 ` Jérémie Dimino
  1 sibling, 0 replies; 4+ messages in thread
From: David Rajchenbach-Teller @ 2009-03-26 17:29 UTC (permalink / raw)
  To: Tiphaine Turpin; +Cc: caml-list

Yeah, the behaviour of *printf is weird in this respect (and that of
Extlib's printf is even a tad worse). For this reason, in Batteries
Included, we have 
 Printf.sprintf  (which behaves as the base Printf.sprintf)
and
 Printf.sprintf2 (which behaves as you expected)

All of this in addition to our [Print] module, which offers a syntax
comparable with [Printf], but allows extending the format rather than
using %a and %t.

Cheers,
 David


On Thu, 2009-03-26 at 17:53 +0100, Tiphaine Turpin wrote:
> Wouldn't it be simpler to have
> two separate directives which accept respectively string printers and
> channel printers, regardless of the outer printing function ?
> 
> Tiphaine
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

-- 
David Teller-Rajchenbach
 Security of Distributed Systems
  http://www.univ-orleans.fr/lifo/Members/David.Teller
   « Ce matin Un crétin A tué un chercheur. » (air connu)
   Latest News of French Research: System being liquidated. Researchers angry.


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

* Re: [Caml-list] printf "%a" vs sprintf "%a"
  2009-03-26 16:53 printf "%a" vs sprintf "%a" Tiphaine Turpin
  2009-03-26 17:29 ` [Caml-list] " David Rajchenbach-Teller
@ 2009-03-26 17:29 ` Jérémie Dimino
  2009-03-26 17:37   ` Tiphaine Turpin
  1 sibling, 1 reply; 4+ messages in thread
From: Jérémie Dimino @ 2009-03-26 17:29 UTC (permalink / raw)
  To: Tiphaine Turpin; +Cc: caml-list

Tiphaine Turpin wrote:
> has type string. But %a breaks this rule. Wouldn't it be simpler to have
> two separate directives which accept respectively string printers and
> channel printers, regardless of the outer printing function ?

Note that you can do it with batteries and the new printf.

The default "%a" directive always uses a channel printer:

# Print.printf p"%a";;
- : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> unit = <fun>
# Print.sprintf p"%a";;
- : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> string = <fun>

And you can define a "%foo" directive which always uses a string printer:

# let printer_foo k f x = k (fun out -> String.print out (f x));;
val printer_as : (('a Batteries.IO.output -> unit) -> 'b) -> ('c -> string) -> 'c -> 'b = <fun>
# Print.printf p"%foo";;
- : ('_a -> string) -> '_a -> unit = <fun>
# Print.sprintf p"%foo";;
- : ('_a -> string) -> '_a -> string = <fun>

Cheers,
Jérémie


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

* Re: [Caml-list] printf "%a" vs sprintf "%a"
  2009-03-26 17:29 ` Jérémie Dimino
@ 2009-03-26 17:37   ` Tiphaine Turpin
  0 siblings, 0 replies; 4+ messages in thread
From: Tiphaine Turpin @ 2009-03-26 17:37 UTC (permalink / raw)
  To: Jérémie Dimino; +Cc: caml-list

Thanks for the replies. sprintf2 is just fine for me.

Tiphaine


Jérémie Dimino a écrit :
> Tiphaine Turpin wrote:
>   
>> has type string. But %a breaks this rule. Wouldn't it be simpler to have
>> two separate directives which accept respectively string printers and
>> channel printers, regardless of the outer printing function ?
>>     
>
> Note that you can do it with batteries and the new printf.
>
> The default "%a" directive always uses a channel printer:
>
> # Print.printf p"%a";;
> - : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> unit = <fun>
> # Print.sprintf p"%a";;
> - : (unit Batteries.IO.output -> '_a -> unit) -> '_a -> string = <fun>
>
> And you can define a "%foo" directive which always uses a string printer:
>
> # let printer_foo k f x = k (fun out -> String.print out (f x));;
> val printer_as : (('a Batteries.IO.output -> unit) -> 'b) -> ('c -> string) -> 'c -> 'b = <fun>
> # Print.printf p"%foo";;
> - : ('_a -> string) -> '_a -> unit = <fun>
> # Print.sprintf p"%foo";;
> - : ('_a -> string) -> '_a -> string = <fun>
>
> Cheers,
> Jérémie
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>   


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

end of thread, other threads:[~2009-03-26 17:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-26 16:53 printf "%a" vs sprintf "%a" Tiphaine Turpin
2009-03-26 17:29 ` [Caml-list] " David Rajchenbach-Teller
2009-03-26 17:29 ` Jérémie Dimino
2009-03-26 17:37   ` Tiphaine Turpin

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