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

Format.bprintf problems #3007

Closed
vicuna opened this issue Nov 2, 2001 · 2 comments
Closed

Format.bprintf problems #3007

vicuna opened this issue Nov 2, 2001 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Nov 2, 2001

Original bug ID: 606
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Ken Wakita
Version: 3.02 and 3.03 alpha
OS: Linux and Cygwin
Submission from: n8nat-9.is.titech.ac.jp (131.112.50.139)

Format.bprintf does not accept "%a" format: bprintf_list1
fails to type check. It seems that O'Caml's type checker presumes
that the output for the formatter is an output channel.

Then I removed all the use of "%a" and got fprintf_list2 and
bprintf_list2. Both compiles well but the latter ignores all the
formatting directives.

If there is a work-around please let me know.

Ken Wakita

let fprintf p fmt = Format.fprintf p fmt
let bprintf b fmt = Format.bprintf b fmt

let rec fprintf_list1 p = function
| [] -> ()
| [x] -> fprintf p "%d" x
| (x :: l) -> fprintf p "%d@;%a" x fprintf_list1 l

let rec fprintf_list2 p = function
| [] -> ()
| [x] -> fprintf p "%d" x
| (x :: l) -> fprintf p "%d@;" x; fprintf_list2 p l

(*
let rec bprintf_list1 b = function
| [] -> ()
| [x] -> bprintf b "%d" x
| (x :: l) -> bprintf b "%d@;%a" x bprintf_list1 l
*)

let rec bprintf_list2 b = function
| [] -> ()
| [x] -> bprintf b "%d" x
| (x :: l) -> bprintf b "%d@;" x; bprintf_list2 b l

let rec upto n =
Array.to_list (Array.mapi (fun i _ -> i) (Array.make n ()))

let _ =
let p = Format.std_formatter in
fprintf p " @[%a@]@." fprintf_list1 (upto 100);

fprintf p " @[";
fprintf_list2 p (upto 100);
fprintf p "@]@.";

let b = Buffer.create 1024 in
bprintf b " @[";
bprintf_list2 b (upto 100);
bprintf b "@]@.";
print_string (Buffer.contents b)

@vicuna
Copy link
Author

vicuna commented Nov 2, 2001

Comment author: administrator

Full_Name: Ken Wakita
Version: 3.02 and 3.03 alpha
OS: Linux and Cygwin
Submission from: n8nat-9.is.titech.ac.jp (131.112.50.139)

Format.bprintf does not accept "%a" format: bprintf_list1
fails to type check. It seems that O'Caml's type checker presumes
that the output for the formatter is an output channel.

Then I removed all the use of "%a" and got fprintf_list2 and
bprintf_list2. Both compiles well but the latter ignores all the
formatting directives.

If there is a work-around please let me know.

Ken Wakita

let fprintf p fmt = Format.fprintf p fmt
let bprintf b fmt = Format.bprintf b fmt

let rec fprintf_list1 p = function
| [] -> ()
| [x] -> fprintf p "%d" x
| (x :: l) -> fprintf p "%d@;%a" x fprintf_list1 l

let rec fprintf_list2 p = function
| [] -> ()
| [x] -> fprintf p "%d" x
| (x :: l) -> fprintf p "%d@;" x; fprintf_list2 p l

(*
let rec bprintf_list1 b = function
| [] -> ()
| [x] -> bprintf b "%d" x
| (x :: l) -> bprintf b "%d@;%a" x bprintf_list1 l
*)

let rec bprintf_list2 b = function
| [] -> ()
| [x] -> bprintf b "%d" x
| (x :: l) -> bprintf b "%d@;" x; bprintf_list2 b l

let rec upto n =
Array.to_list (Array.mapi (fun i _ -> i) (Array.make n ()))

let _ =
let p = Format.std_formatter in
fprintf p " @[%a@]@." fprintf_list1 (upto 100);

fprintf p " @[";
fprintf_list2 p (upto 100);
fprintf p "@]@.";

let b = Buffer.create 1024 in
bprintf b " @[";
bprintf_list2 b (upto 100);
bprintf b "@]@.";
print_string (Buffer.contents b)

I'm not sure that the behaviour you observed can be considered a
bug. It is definitely surprising but I don't see another way to give
some meaing to buffer pretty-printing...

Hence, your message is very interesting since it points out a
semantical problem with formatting in buffers. It may suggest to
suppress the bprintf function for Format, since it is apparently
very unclear to users...

In the mean time, an easy work-around is NOT to use the bprintf
primitive, and use instead the formatter_of_buffer primitive to get a
true formatter out of your buffer. This is semantically clearer and
drastically simplifies your code (it is now useless to define the bprint
versions of your printing routines): just use the original fprintf
versions with the newly created formatter:

let b = Buffer.create 1024 in
(* --> just define a formatter out of your buffer )
let p = Format.formatter_of_buffer b in
(
*)
fprintf p " @[";
fprintf_list2 p (upto 100);
fprintf p "@]@.";
print_string (Buffer.contents b)
;;

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/

@vicuna
Copy link
Author

vicuna commented Nov 2, 2001

Comment author: administrator

Thank you quick answer. I tried to get rid of this problem by using a
temporary file but your suggestion is more elegant.

Thank you.
Ken

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

No branches or pull requests

1 participant