Previous Up Next

Chapter 9  Classes

Classes play two important rôles in Moby: they are the mechanism used to implement objects and they support code reuse and specialization via implementation inheritance.

9.1  Class declarations

ClassDecl
::= class ClassId (: ClassInterface)opt ClassDef
| class ClassId (: ClassInterface)opt = NamedClass
ClassDef
::= { (inherits NamedClass)opt MemberDecl* InitiallyClauseopt }
NamedClass
::= Pathopt ClassId TypeArgsopt

9.2  Member declarations

There three kinds of class members: fields, methods, and makers.
MemberDecl
::= publicopt FieldDecl
| publicopt MethodDecl
| publicopt MakerDecl
Member declarations may be annotated as public, which means that they are visible outside the class (and its subclasses). For fields and methods, the public annotation means that they are visible in the type of the objects generated from the class, while for makers the public annotation means that the maker may be used to generate new objects (cf., Section 11.11.8).

9.2.1  Field declarations

Fields are the instance variables of Moby objects. A field declaration defines the name and type of the field:
FieldDecl
::= field Label : ExtendedType
Unlike many object-oriented languages, fields are immutable by default in Moby. To define a mutable field, one uses the var annotation.

9.2.2  Method declarations

A method declaration is either abstract or concrete:
MethodDecl
::= abstract method Label : TypeScheme
| finalopt overrideopt method Label FunDef
Abstract method declarations are denoted by the keyword abstract and serve as placeholders for a concrete definition to provided by a subclass. If a class has one or more abstract methods, the class is called an abstract class. Abstract classes may not have public makers.

Concrete method declarations are similar to function declarations; they have a function type and body consisting of a block. Concrete methods may be annotated with the final keyword, which means that they may not be overridden by subclasses, and/or with the override keyword, which means that the method declaration is overriding a declaration in its superclass. Inside the body of a method the reserved identifiers self and super are in scope (cf., Section 11.11.9). The reserved identifier self is bound to the object containing the method and is used to accessthe object's fields and methods. The reserved identifier super is used to access super-class methods.

9.2.3  Maker declarations

MakerDecl
::= maker MakerId Params MakerBlock
MakerBlock
::= { MakerStmt (; MakerStmt)* }
| { }
MakerStmt
::= super MakerId Expression
| field Label = Expression
| Statement
Only public makers may be used to create objects (cf., Section 11.11.8).

A maker block must have unit type (i.e., return no results).

9.3  Initially clauses

InitiallyClause
::= initially Expression

An initially clause must have unit type (i.e., return no results).

9.4  Inheritance

One of the primary rôles played by classes is as a mechanism for code reuse and code specialization. This rôle is supported by inheritance.

9.4.1  Method overriding

9.4.2  Object initialization


Previous Up Next