printf
, fprintf
, etc. : all accept the %a
conversion.
The manual says for %a
:
"user-defined printer. Takes two arguments and apply the first one to outchan (the current output channel) and to the second argument. The first argument must therefore have type out_channel -> 'b -> unit and the second 'b. The output produced by the function is therefore inserted in the output of fprintf at the current point."
I can't understand what a user-defined printer is for, and how you would implement and use it. Can someone explain the motivation and maybe provide an example?
For example, when you want to, say, print a complex data-structure, why is it not possible to just print the data-structure with a custom function directly to a string or to output?
sprintf
andksprintf
take a function ofunit -> 'a -> string
unlike the functions you've mentioned. – Barbell%a
instead of just printing to a string (either withprintf
and%s
orsprintf
orksprintf
etc.) and then use the string to do other things - for example to output on a channel. – Friseur