Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005576OCamlstandard librarypublic2012-04-06 12:532013-08-31 12:49
Assigned Toweis 
PlatformOSOS Version
Product Version3.12.1 
Target VersionFixed in Version 
Summary0005576: Missing continuation-passing no-op formatter in Printf
DescriptionAlthough there is a continuation-passing no-op formatting function available as Format.ikfprintf, a corresponding function in Printf seems to be missing. Here is an implementation:

val kiprintf : (unit -> 'a) -> ('b, unit, unit, 'a) format4 -> 'b

let kiprintf k = kapr (fun _ _ -> Obj.magic (k ()))

(Also, I think that the name Printf.ifprintf should be deprecated and renamed to Printf.iprintf since it does not take an out_channel as a parameter.)
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
doligez (administrator)
2012-04-10 16:26

What is the use-case for this function?

You can get a good approximation with:

  let kiprintf k fmt = ksprintf (fun _ -> k ()) fmt;;

It allocates a useless string, but for debugging purposes that's not a big deal.
weis (developer)
2012-04-11 19:40

Granted: ikfprintf was not implemented in module Printf. As in Format, a partial applicatio of ikfprintf would be useful to implement ifprintf, and could be useful to the user as well. Implemented in the current sources for version 4.0 of the compiler.
weis (developer)
2012-04-11 19:46

Printf.ifprintf is not at all deprecated. It is the corresponding ``ignoring function'' for fprintf. In fact, it can take an out_channel as parameter (otherwise it would not be compatible with fprintf). You may have been fooled by the polymorphic type scheme of ifprintf: indeed the type-checker detected that ifprintf is ignoring its channel argument and then assign a polymorphic 'a to this argument; but indeed this 'a could be instanciated to out_channel. Indeed, typical use-case for ifprintf is

let debug fmt =
  if <some condition>
  then fprintf stderr fmt
  else ifprintf stderr fmt

You may read the i in ifprintf as (i)gnoring or (i)f ...
weis (developer)
2012-04-11 19:54

Damien gave you a short (and free of Obj.magic) definition for your kiprintf function. Then he asked for a typical use-case. I would ask for an english explanation of what this function is supposed to do ? I feel it strange to get as argument a continuation with type unit -> 'a.

If indeed you where thinking of a Printf equivalent of Format.ikfprintf, then the typical use-case is the same as for ifprintf, adding continuation in the picture:

let kdebug k fmt =
  if <some condition>
  then kfprintf k stderr fmt
  else ikfprintf k stderr fmt
weis (developer)
2012-04-11 20:28

Printf.ifprintf defined as partial evaluation of ikfprintf.

- Issue History
Date Modified Username Field Change
2012-04-06 12:53 lealanko New Issue
2012-04-10 16:26 doligez Note Added: 0007310
2012-04-10 16:26 doligez Status new => feedback
2012-04-11 19:40 weis Note Added: 0007332
2012-04-11 19:46 weis Note Added: 0007333
2012-04-11 19:54 weis Note Added: 0007334
2012-04-11 19:55 weis Assigned To => weis
2012-04-11 19:55 weis Status feedback => assigned
2012-04-11 20:28 weis Note Added: 0007335
2012-04-11 20:28 weis Status assigned => resolved
2012-04-11 20:28 weis Resolution open => fixed
2013-08-31 12:49 xleroy Status resolved => closed
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker