

Why is virt-builder written in OCaml? -  rwmj
http://rwmj.wordpress.com/2013/11/11/why-is-virt-builder-written-in-ocaml

======
strlen
I love OCaml as well, but haven't seen this before.  It's interesting that
libguestfs provides both object and module style interface and virt-builder
seems to be using the object style:

[http://libguestfs.org/guestfs-ocaml.3.html](http://libguestfs.org/guestfs-
ocaml.3.html)

Overall, great article and great code.  Let's keep in mind that SML is
typically taught to entry-level CS students (some of them with no programming
experience) at universities like CMU, where often a term project might
actually be an ML compiler in ML.  It's not a terribly complex language and
some limitations (which I am not going to state here) aside it has a great use
to play. 

It should be noted that XenServer also uses OCaml.  While it's not really fair
to compare Go with OCaml (the language roots are vastly different as are the
philosophies -- aside from the idea that "static typing shouldn't mean typing
so much you generate static electricity") both seem to do best in the
"userland systems programming" niche -- that awkward place where C++ is
needlessly painful, Java is ill-fitted, and python/etc... simply
inappropriate. 

~~~
rwmj
The guestfs API and bindings in all the languages are generated -- by a large
OCaml program. 

[https://github.com/libguestfs/libguestfs/tree/master/generat...](https://github.com/libguestfs/libguestfs/tree/master/generator)

For an API that has 400 or so calls accessible through a dozen different
programming languages, this makes sense.  The bindings in each language are
never out of date or wrong (except of course they can have a systematic bug,
but those are much easier to fix). 

This also makes it very easy to generate alternate "views" of the same API,
like OCaml module vs OO views. 

------
dsugarman
It is weird to hear someone say that ML is a language only you can program in. 
In my opinion, ML is the most readable code out there.  I used to teach ML and
it was much easier to grade by reading code than to run automated tests. 

~~~
chc
I believe the intended meaning was "a language that nobody else knows" rather
than "a language where code is impenetrable to everyone but the author."  OCaml
is pretty easy, but not if you don't know it. 

~~~
rwmj
Programmers really ought to be fluent in half a dozen programming languages
(that's using a wide definition which includes things like Makefiles and
shell).  OCaml is not exactly the most difficult one.  Compared to things like J
or FORTH, OCaml is pretty simple. 

Also virt-builder is very procedural:
[https://github.com/libguestfs/libguestfs/blob/master/builder...](https://github.com/libguestfs/libguestfs/blob/master/builder/builder.ml)
It's "do this, then run this, and if this condition is true do something
else".  OCaml's strengths here are its brevity, speed and safety.  No
complicated features are used, eg.  no functors, no difficult use of functions
as first-class objects ...

(I'm not actually a fan of functors; I think they obscure the flow of the
code, in the same way as using inheritance in OO). 

~~~
nn3
>Compared to things like J or FORTH, OCaml is pretty simple. 

Huh?  FORTH is one of the most simple programming languages around. 

~~~
rwmj
I wrote JONESFORTH - a literate FORTH interpreter - and I really don't think
it's easy.  It's easy for me now .. having written a FORTH .. but I don't think
that counts as easy for beginners. 

------
avsm
An obvious note here might be that your use of OCaml here predates (for
example) Go by quite a few years.  Continuity is an important consideration,
instead of having to rewrite your code every few years in the latest language
of choice. 

~~~
rwmj
Right, although even if it was a straight choice of Go and OCaml, OCaml would
certainly win since Go has very poor types, no type inference, requires you to
put error handling everywhere.  It's like they didn't understand functional
languages at all.  In fact, I'd just use C instead of Go because at least C is
widely available and well understood. 

~~~
koko775
Hey, there are valid reasons not to use Go, but don't you think you're laying
it on a bit thick?  A lot of those are pretty subjective, too. 

My anecdata tells me that Go's types are sufficient (and sometimes even
handy), error handling everywhere helps more than it hurts (and is easily
ignored if you want to), and is very much _not_ a functional language.  And I
would disagree with the notion that C is, on average, well-understood given
how widely available it is (a nicer way of saying that C is an easy language
for anyone to misunderstand).  Go is comparatively simpler to 'get right' \--
in my experience

Again, this is all very subjective.  Still, I don't see how flaming Go alone
helps highlight OCaml's advantages and contrast their tradeoffs. 

~~~
rwmj
Yes probably I am.  I think this stems from my feeling that Go is a lost
opportunity.  There's room for a sane C++ replacement.  OCaml hasn't made it
yet, although it fits in that space [arguably].  Go tries to do that but
ignores great languages that have gone before it. 

~~~
freyrs3
I understand the confusion about the language though I don't think the goal
was ever to replace C++.  Go still has a managed runtime, so I can't imagine it
would ever appeal to people who want to go purely native. 

------
alexchamberlain
If it is a thin layer around libguestfs, why would Python be slow? 

~~~
rwmj
It likely wouldn't make a measurable difference in the run time.  The main
arguments against using Python are lack of compile-time safety and difficulty
in packaging up something that calls out to C liberally as virt-builder does. 

------
warmfuzzykitten
All those reasons boil down to the first one.  You know and like OCaml (better
than any other language, apparently), so screw everyone else. 

~~~
klibertp
Screw?  I think you're the one who should "hand in your programmer’s card at
the door" \- learning enough OCaml to get started takes half a day and getting
comfortable with it is a matter of weeks at most.  Of course only if you _are_
a programmer, otherwise you might be screwed. 

~~~
warmfuzzykitten
It was exactly that level of arrogance (the last sentence in the original
article) that prompted my first comment.  Really, can't you promote your
language without personal attacks? 

~~~
pessimizer
Really, can't you comment on someone promoting their favorite language without
accusing them of screwing everyone?