|Anonymous | Login | Signup for a new account||2016-06-27 13:19 CEST|
|Main | My View | View Issues | Change Log | Roadmap|
|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0006176||OCaml||OCaml standard library||public||2013-09-13 15:34||2015-12-11 19:25|
|Priority||normal||Severity||feature||Reproducibility||have not tried|
|Target Version||Fixed in Version|
|Summary||0006176: Custom formatters in Format.fprintf and Format.sprintf|
|Description||One of the annoyance of the Format module is that if you define a pretty printer pp_v for your value you can use it with Format.fprintf "%a" pp_v v but not *directly* with Format.sprintf "%a" pp_v v. |
Usually I work around by having a to_string function generated from my pp_v as follows:
let to_string_of_formatter pp v = (* NOT thread safe. *)
Format.fprintf Format.str_formatter "%a" pp v;
And then do a Format.sprintf "%s" (to_string v).
However it would be simpler to have a function Format.spp, defined as follows:
let spp pp_v () v = (* NOT thread safe. *)
Format.fprintf Format.str_formatter "%a" pp_v v;
That would allow to write Format.sprintf "%a" (Format.spp pp_v) v
P.S. Maybe Format.spp should not use Format.str_formatter, I was surprised it worked, thought the flush would trip the Format.sprintf.
|Tags||No tags attached.|
I've also been annoyed by this in the past.
Perhaps, `spp` should be defined as something like:
let spp pp_v () v =
let b = Buffer.create 16 in
let ppf = formatter_of_buffer b in
pp_v ppf v;
pp_flush_queue ppf false;
|Thanks to commit faf9cb478d1b2e by Pierre Weiss which wasn't mentioned in the release notes, I happily discovered that this issue is now fixed in 4.01.0 by using Format.asprintf.|
> Thanks to commit faf9cb478d1b2e by Pierre Weiss which wasn't mentioned in the release notes, I happily discovered that this issue is now fixed in 4.01.0 by using Format.asprintf.
Good catch! Indeed I missed this change when preparing the release notes, as I started from the CHANGES which Pierre forgot to update alongside this commit.
|2013-09-13 15:34||dbuenzli||New Issue|
|2013-09-13 15:55||lpw25||Note Added: 0010350|
|2014-03-12 01:51||dbuenzli||Note Added: 0011037|
|2014-03-13 10:02||gasche||Note Added: 0011039|
|2014-03-13 10:02||gasche||Status||new => resolved|
|2014-03-13 10:02||gasche||Resolution||open => fixed|
|2014-03-13 10:02||gasche||Assigned To||=> gasche|
|2015-12-11 19:25||xleroy||Status||resolved => closed|
|Copyright © 2000 - 2011 MantisBT Group|