Idiomatic ocaml

Can i consider this as idiomatic ocaml ?
Feel free to add your idiomatic ocaml.

let (_ : unit) =
    let (_ : unit) = print_string "Hello" in
    print_string " to the ";
    print_string "World"

Not sure about the Dutch, but I’d probably write:

let () = Format.printf "Hello\n to the \n world\n%!"
1 Like

Interesting, why printf in this case? print_endline would do the trick, right?

I don’t really use print_endline in my code. The Format module is no harder to start with and gives me more flexibility as my messages increase in complexity.


And the fmt module is a lovely repackaging of Format with a ton of combinators to make formatting complex data both succinct and composable.

A small addition to preserve the exact same behaviour as print_endline (the “@.” flush formatter):

let () = Format.printf "Hello\n to the \n world\n%!@."

Particularly useful if you’re doing printf debugging and you want your output to be visible even if the rest of the execution of the program is interrupted via an uncaught exception, as normally Printf’s buffering means that outputs may have not been printed.


I specifically used \n%! to print a newline character and flush instead of @. because the latter also has the effect of closing all open Format boxes.


Ah, missed the %!, I typically use @., but you’re right, the closing of Format boxes can be a bit annoying, good point.

Aaaaanad just to keep going with the beauty …

 to the 

Ha! [there are spaces in there at the ends of the 2nd and 3rd lines.]