Classes in Object ML via Modules

This paper describes a simple means for encoding classes in O bject ML (OML) [RR96] using the Standard ML (SML) module system [Mac86]. In previous work [RR96] , we showed how to mimic class-based implementation inheritance in some simple cases, but we did not know how to extend that to all methods (we had problems with methods that return “self”). This pape r describes a general encoding of class-based implementation inheritance in OML, using the SML module sys tem [Mac86]. In addition to handling all methods, we also show how to encode other various class-base d features, such as static members, protected members, and multiple inheritance. OML extends SML with support for simple objects and subtypin g. Following Abadi and Cardelli [AC95], objects are constructed directly, and methods take a “self” parameter that, upon method invocation, is bound to the object itself. In contrast to Abadi and Cardelli's sys tem, OML requires declarations of the subtyping hierarchy and the interface types of objects. These declara tions also introduce unique object constructors for each object type, which allow straightforward type infe rence (much the way that datatype constructors allow inference of recursive types). OML does not, however, directly support implementation inheritance. Our thesis is that the rich scoping discipline provided by SM L's module system has enough power to encode various aspects of the class systems of C ++ and other languages. The paper is organized as follows. First we give a brief intro duction to OML1 and present a running example. Then Section 3 describes an encoding that works for objects with methods whose types do not involve the type of “self.” In this encoding, classes are str uctures. We then describe how to extend the technique to objects whose interface type includes referen c s to the type of “self.” Section 4 describes how instance variables can be added to the class encoding. Secti on 5 describes how other features of C ++' s class system can be encoded, and how the flexibility of modules adds to the expressive power of the class system.