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
Feature Request: CPS version of sprintf #3321
Comments
Comment author: administrator
This is already done in the current working version of ocaml, although not val kprintf : (string -> string) -> ('a, unit, string) format -> 'a Thanks for the suggestion, -- Damien |
Comment author: administrator Damien Doligez wrote:
Maybe one day I will come up with an original idea! A slight amendment to the type I proposed (I think the Cheers, |
Comment author: administrator
This is exactly what I tried at first, but it doesn't work with %a Consider this piece of code: kprintf outfun "%a" fmtfun argument The proper solution would be to add a fourth argument to the "format" -- Damien |
Original bug ID: 1131
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Bug description
Full_Name: Chris Quinn
Version:
OS:
Submission from: 212.42.169.63 (212.42.169.63)
It would be nice to have the ability to create functions akin to Printf.sprintf
which take multiple arguments, and do something with the resulting string.
The only way to do it is by making sprintf the result, ie:
let f (fmt: ('a,unit,string)) =
... do anything ...
in Printf.sprintf fmt
but then there is no access to the string it eventually produces!
The situation in which I have found a need is in interfacing to a C library
which has its version of printf for its own particular sort of channel type.
I guess this applies just as well to caml based libraries which have functions
that do arbitrary things with the conversion result.
I would like to propose adding a function to the Printf module that
is a very minor variation on sprintf which accepts a continuation for the
string result.
Since the work of format conversion is of course done in caml , that leaves
little
to do for any C-lib based version, but there's no harm in that.
I tested the function below to see if it actually invoked the continuation
on partial applications. It did not - as required.
let cprintf : (string -> 'b) -> ('a, unit, unit) format -> 'a = fun f fmt ->
let fmt = (Obj.magic fmt : string) in
let len = String.length fmt in
let dest = Buffer.create (len + 16) in
let rec doprn i =
if i >= len then begin
let res = Buffer.contents dest in
Buffer.clear dest; (* just in case sprintf is partially applied *)
-> (Obj.magic f res)
end else
...... unchanged
The text was updated successfully, but these errors were encountered: