In standard ML, I have a function that is written to BinIO.outstream, and I would like it to be written to standard output.
While the structure TextIOis of stdOuttype TextIO.outstream, BinIOdoes not have such a variable, and is TexIO.outstreamnot compatible with BinIO.outstream:
- f;
val it = fn : BinIO.outstream -> unit
- TextIO.stdOut;
val it = - : TextIO.outstream
- f TextIO.stdOut;
stdIn:6.1-6.16 Error: operator and operand don't agree [tycon mismatch]
operator domain: BinIO.outstream
operand: TextIO.outstream
in expression:
f TextIO.stdOut
Now, what is the easiest way to convert TextIO.outstreamto BinIO.outstream? those. How to implement ???below?
- f (??? TextIO.stdOut);
Update:
For those who are interested, here is the implementation according to Andreas answer:
fun textWriterToBinWriter (TextPrimIO.WR { name,
chunkSize,
writeVec,
writeArr,
writeVecNB,
writeArrNB,
block,
canOutput,
getPos,
setPos,
endPos,
verifyPos,
close,
ioDesc }) =
let
fun convertWriteVec textWriteVec =
textWriteVec o CharVectorSlice.full o Byte.unpackStringVec
fun convertWriteArr textWriteArr =
textWriteArr o CharArraySlice.full o CharArray.fromList o explode o Byte.unpackString
in
BinPrimIO.WR {
name = name,
chunkSize = chunkSize,
writeVec = Option.map convertWriteVec writeVec,
writeArr = Option.map convertWriteArr writeArr,
writeVecNB = Option.map convertWriteVec writeVecNB,
writeArrNB = Option.map convertWriteArr writeArrNB,
block = block,
canOutput = canOutput,
getPos = getPos,
setPos = setPos,
endPos = endPos,
verifyPos = verifyPos,
close = close,
ioDesc = ioDesc }
end
fun textStreamToBinStream' textStream =
let
val (textWriter, bufferMode) = TextIO.StreamIO.getWriter textStream
in
BinIO.StreamIO.mkOutstream (textWriterToBinWriter textWriter, bufferMode)
end
fun textStreamToBinStream textStream =
let
val textStream' = TextIO.getOutstream textStream
in
BinIO.mkOutstream (textStreamToBinStream' textStream')
end