Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 649

OCaml combine fprintf and sprintf

$
0
0

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.


Viewing all articles
Browse latest Browse all 649

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>