Consider the following code snippet :
type wrapped_int=Wr of int;;
let print_wrapped_int (Wr i)=
"\xe3\x80\x90 "^(string_of_int i)^" \xe3\x80\x91";;
let print_out_wrapped_int (fmt:Format.formatter) x=
Format.fprintf fmt "@[%s@]" (print_wrapped_int x);;
#install_printer print_out_wrapped_int;;
let example1=Wr 7;;
let example2=print_string(print_wrapped_int example1);;
The output in utop is as follows :
$ utop --------+-------------------------------------------------------------+--------
| Welcome to utop version 2.0.1 (using OCaml version 4.04.1)! |
+-------------------------------------------------------------+
Type #utop_help for help about using utop.
-( 15:43:29 )-< command 0 >-------------------------------------{ counter: 0 }-
utop # type wrapped_int=Wr of int;;
type wrapped_int = Wr of int
-( 15:43:32 )-< command 1 >-------------------------------------{ counter: 0 }-
utop #
let print_wrapped_int (Wr i)=
"\xe3\x80\x90 "^(string_of_int i)^" \xe3\x80\x91";;
val print_wrapped_int : wrapped_int -> string = <fun>
-( 15:43:43 )-< command 2 >-------------------------------------{ counter: 0 }-
utop #
let print_out_wrapped_int (fmt:Format.formatter) x=
Format.fprintf fmt "@[%s@]" (print_wrapped_int x);;
val print_out_wrapped_int : Format.formatter -> wrapped_int -> unit = <fun>
-( 15:43:43 )-< command 3 >-------------------------------------{ counter: 0 }-
utop #
#install_printer print_out_wrapped_int;;
-( 15:43:43 )-< command 4 >-------------------------------------{ counter: 0 }-
utop #
let example1=Wr 7;;
val example1 : wrapped_int = ? 7 ? -
( 15:43:43 )-< command 5 >-------------------------------------{ counter: 0 }-
utop # let example2=print_string(print_wrapped_int example1);;
ă 7 ăval example2 : unit = ()
Here I am not satisfied with the display for example1
, as the non-ASCII characters are rendered as a mere ?
. I know however that my terminal is fully capable of displaying the characters correctly, as shown by example2
.
Is this an inescapable limitation of pretty-printing in OCaml, or is there a workaround ?