# Printf @@deriving sexp

**URL:** <https://discuss.ocaml.org/t/printf-deriving-sexp/11517>\
**Category:** Learning\
**Created:** [February 26, 2023, 5:08am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517 "2023-02-26T05:08:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [February 26, 2023, 5:08am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/1 "2023-02-26T05:08:43Z")

</div>

For a type `t` with `@@deriving sexp` is there a simple way to print `x:t` via `printf` ?

EDIT: I’m basically looking for Rust’s `#[deriving(Show)]`

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [February 26, 2023, 5:12am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/2 "2023-02-26T05:12:39Z")

</div>

There is `@@deriving show` ppx that generates `val pp: Format.formatter -> 'a -> unit` that can be combined with `Format.printf`.

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [February 26, 2023, 5:22am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/4 "2023-02-26T05:22:54Z")

</div>

If you want to print S-expressions directly, you can use a combination of Sexp.pp and converting to sexp.

Give me a sec, let me dig up an example.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [February 26, 2023, 5:27am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/5 "2023-02-26T05:27:06Z")

</div>

Yeah, Sexp.to\_string [Data Serialization with S-Expressions - Real World OCaml](https://dev.realworldocaml.org/data-serialization.html)

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [February 26, 2023, 5:28am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/6 "2023-02-26T05:28:32Z")

</div>

You can avoid going to string as an intermediary if you use the pp functions:

```ocaml
open Core

type data = Leaf of int | Node of data list
[@@deriving sexp]

type t = {
  my_int: int;
  my_float: float;
  my_data: data
} [@@deriving sexp]

let instance = {my_int=1; my_float=1.0; my_data=Leaf 0}

let () = Format.printf "%a" Sexp.pp_hum ([%sexp_of: t] instance)
(* outputs: ((my_int 1) (my_float 1) (my_data (Leaf 0))) *)

```

---

<div class="post-metadata">

**Author:** ![roddy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/roddy/32/2261_2.png) [@roddy](https://discuss.ocaml.org/u/roddy)\
**Post date:** [February 26, 2023, 11:57pm UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/7 "2023-02-26T23:57:41Z")

</div>

If you’re using the right Jane Street ppxes (`ppx_jane` will include them), you can do this:

```auto
let () = printf !"Foo: %{sexp:Foo.t} and Bar: %{Bar}\n" foo bar

```

if `Foo.sexp_of_t` and `Bar.to_string` are defined (note the `!` before the format string).

Other options are

```auto
let () = print_s [%message "Foo:" (foo: Foo.t)]

```

or

```auto
let () = print_s ([%sexp_of: Foo.t] foo)
```

---

<div class="post-metadata">

**Author:** ![lukstafi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lukstafi/32/3442_2.png) [@lukstafi](https://discuss.ocaml.org/u/lukstafi)\
**Post date:** [February 28, 2023, 9:38am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/8 "2023-02-28T09:38:49Z")

</div>

Wow, I wasn’t aware of all this functionality. Would anyone be willing to update [the debugging tutorial](https://ocaml.org/docs/debugging) with a section(s) about printf debugging, native code tracing, [`ppx_debug`](https://github.com/dariusf/ppx_debug)?

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [March 2, 2023, 6:00am UTC](https://discuss.ocaml.org/t/printf-deriving-sexp/11517/9 "2023-03-02T06:00:42Z")

</div>

Ah, yes, forgot to mention earlier, but alongside these, @dariusf also has [GitHub - dariusf/ppx\_interact: Interactive breakpoints!](https://github.com/dariusf/ppx_interact), which provides Python-style interactive breakpoints by compiling a toploop into the program:

```ocaml
let () =
  let xs = [1; 2; 3] in
  let f (a : int) =
    [%interact]
  in
  print_endline "hello!";
  f 2;
  print_endline "goodbye!"

```

It’s only implemented for bytecode, but I can’t see why it couldn’t also be ported to native code as well.
