Previous Up Next

6.9  Classes

Classes are defined using a small language, similar to the module language.

6.9.1  Class types

Class types are the class-level equivalent of type expressions: they specify the general shape and type properties of classes.

class-type::= [[?]label-name:]  typexpr ->  class-type  
class-body-type::= object [( typexpr )]  {class-field-specend  
   [[ typexpr  {, typexpr]]  classtype-path  
class-field-spec::= inherit class-body-type  
   val [mutable] [virtualinst-var-name :  typexpr  
   val virtual mutable inst-var-name :  typexpr  
   method [private] [virtualmethod-name :  poly-typexpr  
   method virtual private method-name :  poly-typexpr  
   constraint typexpr =  typexpr

Simple class expressions

The expression classtype-path is equivalent to the class type bound to the name classtype-path. Similarly, the expression [ typexpr1 , …  typexprn ]  classtype-path is equivalent to the parametric class type bound to the name classtype-path, in which type parameters have been instantiated to respectively typexpr1, …typexprn.

Class function type

The class type expression typexpr ->  class-type is the type of class functions (functions from values to classes) that take as argument a value of type typexpr and return as result a class of type class-type.

Class body type

The class type expression object [( typexpr )]  {class-field-spec} end is the type of a class body. It specifies its instance variables and methods. In this type, typexpr is matched against the self type, therefore providing a name for the self type.

A class body will match a class body type if it provides definitions for all the components specified in the class body type, and these definitions meet the type requirements given in the class body type. Furthermore, all methods either virtual or public present in the class body must also be present in the class body type (on the other hand, some instance variables and concrete private methods may be omitted). A virtual method will match a concrete method, which makes it possible to forget its implementation. An immutable instance variable will match a mutable instance variable.


The inheritance construct inherit class-body-type provides for inclusion of methods and instance variables from other class types. The instance variable and method types from class-body-type are added into the current class type.

Instance variable specification

A specification of an instance variable is written val [mutable] [virtual] inst-var-name :  typexpr, where inst-var-name is the name of the instance variable and typexpr its expected type. The flag mutable indicates whether this instance variable can be physically modified. The flag virtual indicates that this instance variable is not initialized. It can be initialized later through inheritance.

An instance variable specification will hide any previous specification of an instance variable of the same name.

Method specification

The specification of a method is written method [private] method-name :  poly-typexpr, where method-name is the name of the method and poly-typexpr its expected type, possibly polymorphic. The flag private indicates that the method cannot be accessed from outside the object.

The polymorphism may be left implicit in public method specifications: any type variable which is not bound to a class parameter and does not appear elsewhere inside the class specification will be assumed to be universal, and made polymorphic in the resulting method type. Writing an explicit polymorphic type will disable this behaviour.

If several specifications are present for the same method, they must have compatible types. Any non-private specification of a method forces it to be public.

Virtual method specification

A virtual method specification is written method [private] virtual method-name :  poly-typexpr, where method-name is the name of the method and poly-typexpr its expected type.

Constraints on type parameters

The construct constraint typexpr1 =  typexpr2 forces the two type expressions to be equal. This is typically used to specify type parameters: in this way, they can be bound to specific type expressions.

6.9.2  Class expressions

Class expressions are the class-level equivalent of value expressions: they evaluate to classes, thus providing implementations for the specifications expressed in class types.

class-expr::= class-path  
   [ typexpr  {, typexpr]  class-path  
   ( class-expr )  
   ( class-expr :  class-type )  
   class-expr  {argument}+  
   fun {parameter}+ ->  class-expr  
   let [reclet-binding  {and let-bindingin  class-expr  
   object class-body end  
class-field::= inherit class-expr  [as lowercase-ident]  
   val [mutableinst-var-name  [: typexpr=  expr  
   val [mutablevirtual inst-var-name :  typexpr  
   val virtual mutable inst-var-name :  typexpr  
   method [privatemethod-name  {parameter}  [: typexpr=  expr  
   method [privatemethod-name :  poly-typexpr =  expr  
   method [privatevirtual method-name :  poly-typexpr  
   method virtual private method-name :  poly-typexpr  
   constraint typexpr =  typexpr  
   initializer expr

Simple class expressions

The expression class-path evaluates to the class bound to the name class-path. Similarly, the expression [ typexpr1 , …  typexprn ]  class-path evaluates to the parametric class bound to the name class-path, in which type parameters have been instantiated respectively to typexpr1, …typexprn.

The expression ( class-expr ) evaluates to the same module as class-expr.

The expression ( class-expr :  class-type ) checks that class-type matches the type of class-expr (that is, that the implementation class-expr meets the type specification class-type). The whole expression evaluates to the same class as class-expr, except that all components not specified in class-type are hidden and can no longer be accessed.

Class application

Class application is denoted by juxtaposition of (possibly labeled) expressions. It denotes the class whose constructor is the first expression applied to the given arguments. The arguments are evaluated as for expression application, but the constructor itself will only be evaluated when objects are created. In particular, side-effects caused by the application of the constructor will only occur at object creation time.

Class function

The expression fun [[?]label-name:pattern ->  class-expr evaluates to a function from values to classes. When this function is applied to a value v, this value is matched against the pattern pattern and the result is the result of the evaluation of class-expr in the extended environment.

Conversion from functions with default values to functions with patterns only works identically for class functions as for normal functions.

The expression

fun parameter1 …  parametern ->  class-expr

is a short form for

fun parameter1 ->fun  parametern ->  expr

Local definitions

The let and let rec constructs bind value names locally, as for the core language expressions.

If a local definition occurs at the very beginning of a class definition, it will be evaluated when the class is created (just as if the definition was outside of the class). Otherwise, it will be evaluated when the object constructor is called.

Class body

class-body::=  [( pattern  [: typexpr)]  { class-field }

The expression object class-body end denotes a class body. This is the prototype for an object : it lists the instance variables and methods of an objet of this class.

A class body is a class value: it is not evaluated at once. Rather, its components are evaluated each time an object is created.

In a class body, the pattern ( pattern  [: typexpr] ) is matched against self, therefore providing a binding for self and self type. Self can only be used in method and initializers.

Self type cannot be a closed object type, so that the class remains extensible.

Since OCaml 4.01, it is an error if the same method or instance variable name is defined several times in the same class body.


The inheritance construct inherit class-expr allows reusing methods and instance variables from other classes. The class expression class-expr must evaluate to a class body. The instance variables, methods and initializers from this class body are added into the current class. The addition of a method will override any previously defined method of the same name.

An ancestor can be bound by appending as lowercase-ident to the inheritance construct. lowercase-ident is not a true variable and can only be used to select a method, i.e. in an expression lowercase-ident #  method-name. This gives access to the method method-name as it was defined in the parent class even if it is redefined in the current class. The scope of this ancestor binding is limited to the current class. The ancestor method may be called from a subclass but only indirectly.

Instance variable definition

The definition val [mutable] inst-var-name =  expr adds an instance variable inst-var-name whose initial value is the value of expression expr. The flag mutable allows physical modification of this variable by methods.

An instance variable can only be used in the methods and initializers that follow its definition.

Since version 3.10, redefinitions of a visible instance variable with the same name do not create a new variable, but are merged, using the last value for initialization. They must have identical types and mutability. However, if an instance variable is hidden by omitting it from an interface, it will be kept distinct from other instance variables with the same name.

Virtual instance variable definition

A variable specification is written val [mutable] virtual inst-var-name :  typexpr. It specifies whether the variable is modifiable, and gives its type.

Virtual instance variables were added in version 3.10.

Method definition

A method definition is written method method-name =  expr. The definition of a method overrides any previous definition of this method. The method will be public (that is, not private) if any of the definition states so.

A private method, method private method-name =  expr, is a method that can only be invoked on self (from other methods of the same object, defined in this class or one of its subclasses). This invocation is performed using the expression value-name #  method-name, where value-name is directly bound to self at the beginning of the class definition. Private methods do not appear in object types. A method may have both public and private definitions, but as soon as there is a public one, all subsequent definitions will be made public.

Methods may have an explicitly polymorphic type, allowing them to be used polymorphically in programs (even for the same object). The explicit declaration may be done in one of three ways: (1) by giving an explicit polymorphic type in the method definition, immediately after the method name, i.e. method [private] method-name :  {' ident}+ .  typexpr =  expr; (2) by a forward declaration of the explicit polymorphic type through a virtual method definition; (3) by importing such a declaration through inheritance and/or constraining the type of self.

Some special expressions are available in method bodies for manipulating instance variables and duplicating self:

expr::= …  
  inst-var-name <-  expr  
  {< [ inst-var-name =  expr  { ; inst-var-name =  expr }  [;] ] >}

The expression inst-var-name <-  expr modifies in-place the current object by replacing the value associated to inst-var-name by the value of expr. Of course, this instance variable must have been declared mutable.

The expression {< inst-var-name1 =  expr1 ;;  inst-var-namen =  exprn >} evaluates to a copy of the current object in which the values of instance variables inst-var-name1, …,  inst-var-namen have been replaced by the values of the corresponding expressions expr1, …,  exprn.

Virtual method definition

A method specification is written method [private] virtual method-name :  poly-typexpr. It specifies whether the method is public or private, and gives its type. If the method is intended to be polymorphic, the type must be explicitly polymorphic.

Constraints on type parameters

The construct constraint typexpr1 =  typexpr2 forces the two type expressions to be equals. This is typically used to specify type parameters: in that way they can be bound to specific type expressions.


A class initializer initializer expr specifies an expression that will be evaluated whenever an object is created from the class, once all its instance variables have been initialized.

6.9.3  Class definitions

class-definition::= class class-binding  { and class-binding }  
class-binding::= [virtual] [[ type-parameters ]]  class-name  {parameter}  [: class-type]  =  class-expr  
type-parameters::= ' ident  { , ' ident }

A class definition class class-binding  { and class-binding } is recursive. Each class-binding defines a class-name that can be used in the whole expression except for inheritance. It can also be used for inheritance, but only in the definitions that follow its own.

A class binding binds the class name class-name to the value of expression class-expr. It also binds the class type class-name to the type of the class, and defines two type abbreviations : class-name and # class-name. The first one is the type of objects of this class, while the second is more general as it unifies with the type of any object belonging to a subclass (see section 6.4).

Virtual class

A class must be flagged virtual if one of its methods is virtual (that is, appears in the class type, but is not actually defined). Objects cannot be created from a virtual class.

Type parameters

The class type parameters correspond to the ones of the class type and of the two type abbreviations defined by the class binding. They must be bound to actual types in the class definition using type constraints. So that the abbreviations are well-formed, type variables of the inferred type of the class must either be type parameters or be bound in the constraint clause.

6.9.4  Class specifications

class-specification::= class class-spec  { and class-spec }  
class-spec::= [virtual] [[ type-parameters ]]  class-name :  class-type

This is the counterpart in signatures of class definitions. A class specification matches a class definition if they have the same type parameters and their types match.

6.9.5  Class type definitions

classtype-definition::= class type classtype-def  { and classtype-def }  
classtype-def::= [virtual] [[ type-parameters ]]  class-name =  class-body-type

A class type definition class class-name =  class-body-type defines an abbreviation class-name for the class body type class-body-type. As for class definitions, two type abbreviations class-name and # class-name are also defined. The definition can be parameterized by some type parameters. If any method in the class type body is virtual, the definition must be flagged virtual.

Two class type definitions match if they have the same type parameters and they expand to matching types.

Previous Up Next