In OCaml, how can I create an out_channel which writes to a string/buffer instead of a file on disk

Posted by Tianyi Cui on Stack Overflow See other posts from Stack Overflow or by Tianyi Cui
Published on 2012-09-22T03:34:55Z Indexed on 2012/09/22 3:37 UTC
Read the original article Hit count: 180

Filed under:
|

I have a function of type in_channel -> out_channel -> unit which will output something to an out_channel. Now I'd like to get its output as a string. Creating temporary files to write and read it back seems ugly, so how can I do that? Is there any other methods to create out_channel besides Pervasives.open_out family?

Actually, this function implemented a repl. What I really need is to test it programmatically, so I'd like to first wrap it to a function of type string -> string. For creating the in_channel, it seems I can use Scanf.Scanning.from_string, but I don't know how to create the out_channel parameter.

© Stack Overflow or respective owner

Related posts about io

Related posts about ocaml