I think if you propose beginner to use a ppx (or anything that isnt in the stdlib), you need to explain how to install the dependency (with opam, but the package names should be given), and also how to build your project with it. Maybe also explain the difference between adding the dep to a dune file and adding the dep to a .opam file. This should be in a section that is easy to ignore if you know how to do this (maybe at the end ?)
Good point. Let’s do a separate cookbook page for dependencies installation, and cite it.
Also we need a brief explanation of ppx, and recommended readings from the documentation.
More than once while using printf-debugging in the past I’ve been bitten by OCaml’s buffered output. As a consequence I now always flush, to make sure the output is printed - and not sending me on a wild goose chase:
let i = 20 in
Printf.printf "At line 20 - and variable i is %i\n%!" i
(you can also use flush stdout rather than the %! specifier)
I believe this is a relatively common tripwire and hence should be included.
When debugging I typically don’t add ppx-dependencies to print values, but maybe thats just me…
Also if you are using Format, the directive to use is "@." or "@?" (for a direct equivalent of "%!").
Also I’d just mention that there have been a lot of discussions upstream on making stderr unbuffered by default. But I think the change hasn’t been made so far. The last occurence may be have been this PR.
does it work with other data structures and types?
Sorry, I don’t understand your question.
[@@deriving show] will auto-generate a “to-string” function for you. It doesn’t print anything by itself. To “debug print a value” you will need something like print_string, print_endline or Printf.printf to pass the resulting string to.
When I debug I tend to either print simpler values, already have a “to-string” function available for more complex types, or quickly cook one up out of combinators from QCheck’s Print module (but that’s probably a consequence of my current activities…)
This issue can arise whenever you use printf.
Yes, print_endline does a flush stdout at the end, so that is a good choice. However something like print_string doesn’t flush either - so it is not just a printf issue. I believe we agree to recommend using a flushing printer (either print_endline, printf "...%!", or …) when printf debugging…
One thing to note about Format and flushing is that functions that accept a formatter as an argument should generally not do any flushing. Instead, the final call where e.g. Format.err_formatter is passed in should include a final flush. Flushing force-closes any open formatting boxes, so functions that accept a formatter and flush it are not usable in any context intended to produce formatted output.
Instead of documenting Format, I would like to suggest documenting Fmt: it’s much more accessible, and the many different type-combinators are really useful for making debug-printing easy. And since one already must have ppx_deriving loaded, it’s not much to have fmt also.