I want to write a function that can either print something to a string or to an out_channel
. I'm stuck with the internal format
stuff however.
My approach is something like this:
type pr_target = Channel of out_channel | StrRef of string reflet my_print target fmt = match target with | Channel ch -> fprintf ch fmt | StrRef str -> str := !str ^ sprintf fmt
This gives a type errors as the two uses of fmt
are different: fprintf
takes an ('a, out_channel, unit) format
while sprintf
takes an ('a, unit, string) format
.
How can I write such a function? I'm not super proficient with modules or OCamls polymorphism.