Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006176OCamlOCaml standard librarypublic2013-09-13 15:342014-03-13 10:02
Reporterdbuenzli 
Assigned Togasche 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0006176: Custom formatters in Format.fprintf and Format.sprintf
DescriptionOne 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;
  Format.flush_str_formatter ()

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;
  Format.flush_str_formatter ()

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.
 

TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0010350)
lpw25 (developer)
2013-09-13 15:55

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;
      Buffer.contents b
(0011037)
dbuenzli (reporter)
2014-03-12 01:51

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.
(0011039)
gasche (developer)
2014-03-13 10:02

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

- Issue History
Date Modified Username Field Change
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


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker