The connection _ state type is
a simple enumeration of three named
states that the connection can be in;
connection _ info is a record type
containing a number of fields describing different aspects of a connection.
Note that the fields that have option
at the end of the type are essentially
nullable fields. (By default, values in
OCaml are guaranteed to be non-null).
Otherwise, there is nothing about this
code that is all that different from what
you might write in Java or C#.
Here is some information on the individual record fields and how they relate to each other:
˲ ˲ server indicates the identity of
the server on the other side of the connection.
˲ ˲ last _ ping _ time and last _
ping _ id are intended to be used
as part of a keep-alive protocol. Note
that either both of those fields should
be present, or neither of them should.
Also, they should be present only when
state is Connected.
˲ ˲ The session _ id is a unique
identifier that is chosen afresh every
time the connection is reestablished. It
also should be present only when state
is Connected.
˲ ˲ when _ initiated is for keeping track of when the attempt to start
the connection began, which can be
used to determine when the attempt
to connect should be abandoned. This
should be present only when state is
Connecting.
˲ ˲ when _ disconnected keeps
track of when the connection entered
the Disconnected state, and should
be present only in that state.
As you can see, a number of invariants tie the different record fields together. Maintaining such invariants
takes real work. You need to document
them carefully so you do not trip over
them later; you need to write tests to
verify the invariants; and you must exercise continuing caution not to break
the invariants as the code evolves.
But we can do better. The rewrite in
Figure 5 uses a combination of prod-
uct and sum types that more precisely
represents the set of allowable states
of a connection. In particular, there is
a different record type for each of the
three states, each containing the in-
formation that is relevant just to that
state. Information that is always rel-
evant (in this case, just the server)
is pushed to the top-level record. Also,
we have made it explicit that last _
ping _ time and last _ ping _ id
are either both present or both absent
by representing them as last _ ping,
which is an optional pair.
Limitations
None of this is to say that OCaml is
without its flaws. There are, of course,
all of the problems associated with being a minority language. OCaml has a
great community that has generated a
rich set of libraries, but that collection
of libraries pales in comparison with
what is available for Python, C, or Java.
Similarly, development tools such
as IDEs, profilers, and debuggers are
there, but are considerably less mature and feature-full than their cousins in more mainstream languages.
Another limitation of OCaml has to
do with parallelism. The OCaml run-time has a single runtime lock, which
means that one must use multiple
processes to take advantage of multiple cores on a single machine. For the
most part, this fits our development
model well: we prefer message passing
to shared-memory threads as a programming model for parallelism, since
it leads to code that is easier to reason
about and it scales better to systems
that cross multiple physical machines.
The tools available in the wider OCaml
world for doing this kind of multipro-cess programming, however, are still
maturing.
But OCaml’s limitations are not fundamental in nature. They have more to
do with the details of the implementation or the popularity of the language
and not with the language itself. In the
end, that is what I find most puzzling.
I am now quite convinced that the core
ideas behind OCaml are enormously
valuable, as evidenced by the fact that
OCaml itself, whatever its limitations,
is a profoundly effective and powerful
tool. Yet, those ideas remain stubbornly outside of the mainstream.
Perhaps this is finally on the verge
of changing. Languages such as F#
and Scala are bringing some of the
ideas behind OCaml and Haskell to a
wider audience by integrating themselves within the Dotnet and Java ecosystems, respectively. Maybe 10 years
from now, we will no longer need to
ask why these ideas have failed to
catch on in the wider world. But there
is no reason to wait. You can add
OCaml to your toolbox now.
Related articles
on queue.acm.org
A Conversation with Arthur Whitney
http://queue.acm.org/detail.cfm?id=1531242
Passing a Language through
the Eye of a needle
Roberto Ierusalimschy,
Luiz Henrique de Figueiredo,
and Waldemar Celes
http://queue.acm.org/detail.cfm?id=1983083
The next Big Thing
George Neville-Neil
http://queue.acm.org/detail.cfm?id=1317398
Yaron Minsky joined Jane street in 2003, where he
founded the quantitative research group. since 2007 he
has managed the firms’s technology group.