Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing continuation-passing no-op formatter in Printf #5576

Closed
vicuna opened this issue Apr 6, 2012 · 5 comments
Closed

Missing continuation-passing no-op formatter in Printf #5576

vicuna opened this issue Apr 6, 2012 · 5 comments

Comments

@vicuna
Copy link

vicuna commented Apr 6, 2012

Original bug ID: 5576
Reporter: lealanko
Assigned to: @pierreweis
Status: closed (set by @xavierleroy on 2013-08-31T10:49:01Z)
Resolution: fixed
Priority: normal
Severity: feature
Version: 3.12.1
Category: standard library

Bug description

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

@vicuna
Copy link
Author

vicuna commented Apr 10, 2012

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Apr 11, 2012

Comment author: @pierreweis

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.

@vicuna
Copy link
Author

vicuna commented Apr 11, 2012

Comment author: @pierreweis

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
then fprintf stderr fmt
else ifprintf stderr fmt
;;

You may read the i in ifprintf as (i)gnoring or (i)f ...

@vicuna
Copy link
Author

vicuna commented Apr 11, 2012

Comment author: @pierreweis

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
then kfprintf k stderr fmt
else ikfprintf k stderr fmt
;;

@vicuna
Copy link
Author

vicuna commented Apr 11, 2012

Comment author: @pierreweis

Printf.ifprintf defined as partial evaluation of ikfprintf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants