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 ::=  
  | class-body-type
  | [[?]label-name:]  typexpr ->  class-type
class-body-type ::= object [( typexpr )]  {class-field-spec} end
  | class-path
  | [ typexpr  {, typexpr} ]  class-path
class-field-spec ::= inherit class-type
  | val [mutable] inst-var-name :  typexpr
  | method [private] method-name :  poly-typexpr
  | method [private] virtual method-name :  poly-typexpr
  | constraint typexpr =  typexpr

Simple class expressions

The expression class-path is equivalent to the class type bound to the name class-path. Similarly, the expression [ typexpr1 , ...  typexprn ]  class-path is equivalent to the parametric class type bound to the name class-path, in which type parameters have been instanciated 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 binding 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 type, and these definitions meet the type requirements given in the class type. Furthermore, all methods either virtual or public present in the class body must also be present in the class 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.

Inheritance


The inheritance construct inherit class-type allows to include methods and instance variables from other classes types. The instance variable and method types from this class type are added into the current class type.

Instance variable specification


A specification of an instance variable is written val [mutable] 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.

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 whether the method can be accessed from outside the class.

The polymorphism may be left implicit in 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 polymorphic, and made explicit in the resulting method type. Writing an explicit polymorphic type will disable this behaviour.

Several specification for the same method must have compatible types. Any non-private specification of a method forces it to be public.

Virtual method specification


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 equals. This is typically used to specify type parameters: they can be that way be bound to a specified type expression.

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 [rec] let-binding  {and let-binding} in  class-expr
  | object [( pattern  [: typexpr] )]  { class-field } end
class-field ::= inherit class-expr  [as value-name]
  | val [mutable] inst-var-name  [: typexpr] =  expr
  | method [private] method-name  {parameter}  [: typexpr] =  expr
  | method [private] method-name :  poly-typexpr =  expr
  | method [private] virtual 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 instanciated to respectively typexpr1, ...typexprn.

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

The expression ( class-expr :  class-type ) checks that class-type match 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. Evaluation works as for expression application.

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.

Class body

The expression object ( pattern  [: typexpr] )  { class-field } 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 provinding 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.

Inheritance


The inheritance construct inherit class-expr allows to reuse 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 methods of the same name.

An ancestor can be bound by prepending the construct as value-name to the inheritance construct above. value-name is not a true variable and can only be used to select a method, i.e. in an expression value-name #  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 an 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. Several variables of the same name can be defined in the same class. The flag mutable allows physical modification of this variable by methods.

An instance variables can only be used in the following methods and initializers of the class.

Method definition


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 current class as well as of subclasses of the current class). 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-name =  expr  { ; inst-var-name =  expr } ] >} 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


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 should be explicit.

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: they can be that way be bound to a specified type expression.

Initializers


A class initializer initializer expr specifies an expression that will be evaluated when an object will be created from the class, once all the 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 specification


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 the types they expand to match.


Previous Up Next