# Pretty printer for custom data types best practices?

**URL:** <https://discuss.ocaml.org/t/pretty-printer-for-custom-data-types-best-practices/6723>\
**Category:** Learning\
**Created:** [November 7, 2020, 10:02am UTC](https://discuss.ocaml.org/t/pretty-printer-for-custom-data-types-best-practices/6723 "2020-11-07T10:02:26Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![progman](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@progman](https://discuss.ocaml.org/u/progman)\
**Post date:** [November 8, 2020, 11:06am UTC](https://discuss.ocaml.org/t/pretty-printer-for-custom-data-types-best-practices/6723/5 "2020-11-08T11:06:06Z")

</div>

the Genprint library uses the compilers’ type representation stored  
in .cmt files to print.  
it will print abstract types too from installed libraries as long as they  
were compiled -bin-annot.  
and here is an excerpt from the test file on creating a custom  
printer for an abstract type:

```auto
  let open Bigarray in
  let open Array1 in
  let a1=create Float32 C_layout 1 in
  set a1 0 99.9;
  (* this is abstract because it resides at the C level *)
  [%pr bigarray abstracted a1];
  let a1print ppf (a1: _ Array1.t) = Format.fprintf ppf "got it...%f" (get a1 0) in
  (* %printer only accepts an identifier, not a closure *)
  [%install_printer a1print];
  (* now uses the printer *)
  [%pr bigarray unmasked a1];
  (* and the value directly *)
  [%pr bigarray content(get a1 0 )];
  (* and remove it... *)
  [%remove_printer a1print];
  [%pr bigarray abstract again a1];

```

the project repo also has a demo of the library integrated into the debugger.

nb. the availability of .cmt files is a runtime requirement, in-line  
with the development focus of the library.

---

_[View the full topic](https://discuss.ocaml.org/t/pretty-printer-for-custom-data-types-best-practices/6723)._
