Next: 6.11.1 Parameterised classes Up: 6. Programming Previous: 6.10 The standard libraries Contents

#
6.11 Classes

You can define new types using `class`. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_plain = class {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `lasagne = "large sheets";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `fusilli = "sort of twisty";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `radiatori = "lots of ridges";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

This defines a new class called `Pasta_plain`. The class has three members (`lasagne`, `fusilli` and `radiatori`), each of which has a list of `char` as its value. By convention, we've named classes with an initial capital letter, but of course you can do what you like.

You can refer to the members of a class using the class project (`.`) operator. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_plain.lasagne == "large sheets"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Classes can contain any objects as members, including functions and sub-classes. Functions may define local classes, classes may define local functions, and all may refer to each other using the usual scope rules. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_all = class {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `Filled = class {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  |  |  |  | `tortelloni = "venus' navel";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  |  |  |  | `ravioli = "square guys";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `plain = Pasta_plain;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

When you define a class, `nip2` adds a few extra members for you. `name` is a list of `char` giving the name of the class. `this` and `super` are the most-enclosing class instance and the class instance this class is derived from (see §6.11.2). `nip2` also adds a default constructor: a member with the same name as the class, pointing back to the class constructor.

For efficiency reasons `nip2` does not allow mutual recursion at the top level. If two functions depend on each other, neither will ever be calculated. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `a = 1 : b;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `b = 2 : a;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Neither `a` nor `b` will have a value.

You can have mutual recursion between class members. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Fred = class {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `a = 1 : b;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `b = 2 : a;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Now `Fred.a` will have the value `[1, 2, 1, 2, 1, ...]`.

Subsections

- 6.11.1 Parameterised classes
- 6.11.2 Inheritance
- 6.11.3 Minor class features

Next: 6.11.1 Parameterised classes Up: 6. Programming Previous: 6.10 The standard libraries Contents