functors vs classes

From: Lyn A Headley (
Date: Thu May 15 1997 - 00:01:48 MET DST

Message-Id: <>
Subject: functors vs classes
Date: Wed, 14 May 1997 17:01:48 -0500
From: Lyn A Headley <>


In the caml programming language we are offered an (unprecedented)
choice between two varieties of modular polymorphism, classes and
functors. Each offers distinct advantages, and yet in a way it seems
that each supports a different _style_ of programming. In my limited
experience it seems that most people opt to focus on functors for
their modularization needs, and make little or no use of the objective
stuff. How do others see this?

In my case I believe that both styles offer advantages, and that it
would behoove the prudent caml programmer to develop a style which
uses each paradigm, in the appropriate situation. The question is,
how can we identify which situation deserves which representation?

Obviously inheritance is a main difference; object-oriented techniques
are appropriate for modeling trees of subclasses descended from a
common base. And yet, so are variant types, as long as the tree
doesn't get too deep. So it seems that if a type could have many
layers of inheritance, maybe an object would be the best
representation. Otherwise, I'd take the conceptual simplicity,
readability and pattern matching that go along with variant types.

While both models offer polymorphism, the form taken is different. It
seems that functors are appropriate when a finite number of program
units depend on or contain each other. Objective polymorphism, on the
other hand, seems most effective (again) when programs contain various
instanciations of objects related hierarchichally.

I'm hoping to get some discussion on this issue since (a) I'd like a
clearer grasp of the material and (b) I imagine this is a common
question for new ocaml programmers, and I think it would be nice for
them if we could flesh out a document that describes the various
qualities and advantages of each paradigm.


This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:11 MET