Unwanted line breaks while printing type using Printtyp

Format.asprintf "%a" Printtyp.type_scheme typ gives me unwanted line breaks if the output is long. What controls line breaking behavior here?

The above returns a string but it appears that the string contains line break characters.

It appears that Printtyp uses box-type pretty printing that generates line breaks. For expediency I just post processed the string to remove '\n's.

It is possible to avoid the postprocessing by using a formatter which emits space rather than newlines when printing. This can be done by updating the formatter functions with pp_get_formatter_out_functions and pp_set_formatter_out_functions:

let without_newline ppf =
  let fns =
    Format.pp_get_formatter_out_functions ppf ()
  in
  let out_newline () = fns.out_spaces 1 in
  let fns = Format.{ fns with out_newline } in
  Format.pp_set_formatter_out_functions ppf fns;
  ppf

let no_newline_string fmt =
  let b = Buffer.create 20 in
  let ppf = Format.formatter_of_buffer b in
  let ppf = without_newline ppf in
  let flush ppf =
      Format.pp_print_flush ppf ();
      Buffer.contents b
  in
  Format.kfprintf flush ppf fmt

let s = no_newline_string "@[<v>%d@ %d@ %d@]" 1 2 3
1 Like