[MLton] Structures inside a function?

Matthew Fluet fluet@cs.cornell.edu
Thu, 20 Jan 2005 22:20:48 -0500 (EST)

> Having mused briefly before seeing your reply, I was going to make another
> point, which was that functors with non-type arguments are in some sense
> suspect, because they cannot be instantiated in a local (expression)
> scope.  I many situations, this isn't a problem; but in some, it really
> can be.  For example, I believe that the Standard ML Basis Library has
> such a problem with regards to the IO stream and sockets; namely, I don't
> believe that there is a way to open a socket (a core-level operation) and
> then lift that socket through the IO functors (a module-level operation)
> to get an IMPERATIVE_IO structure for reading from the socket.  (There is
> a degenerate mechanism by opening the socket at the top-level, but then
> you can't recover from errors nicely.)

I have to retract this comment about the Basis Library, as the book
version shows explicitly how to do this in Listing 10.2.  Interestingly,
the style of the code looks like:

fun mkStreams (sock) =
    val rd = BinPrimIO.RD {...}
    val wr = BinPrimIO.WR {...}
    val inStrm = BinIO.mkInstream(BinIO.StreamIO.mkInstream(rd,...))
    val outStrm = BinIO.mkOutstream(BinIO.StreamIO.mkOutstream(wr,...))

which is very similar to what I proposed: collect all the relevant
functions in a record, then pass it up through mk* functions when they
depend upon a core-level value.

So, this is probably where I had the idea in the back of my mind.