Perfect Developer basic tutorial 2 This page last modified 2011-10-29 (JAC)

Enumeration classes

An enumeration is a class having a finite number of named, constant instances.

For example, you might wish to declare a class called Color whose instances are named red, yellow, orange, green, blue, black, white.

You declare such a class in Perfect like this:

  class Color ^= enum red, yellow, orange, green, blue, black, white end

Because the instances are declared as member constants of the class, you need to give the class name when referring to them, using the syntax name@class as in the following example:

  var colorOfCar: Color; ...
  colorOfCar! = red@Color;

Whenever you declare an enumeration class, the predecessor and successor unary operators < and >, the sequence construction operator .. and the function toString are automatically defined for you.

The predecessor and successor operators yield the previous and next values in the list of instance names respectively.
For example, <yellow@Color yields red@Color, while <red@Color is illegal because red is the lowest value of type Color and therefore has no predecessor.

The usual comparison operators (including the rank operator ~~) are also defined automatically for you. Values in the enum list compare higher than earlier values in the list and lower than later values.
For example, red@Color < black@Color yields true.

Enumeration classes also support the type operators highest and lowest. Using the above example, the expression lowest Color yields red@Color, while highest Color has the value white@Color.

Enumerations are implicitly final classes, so it is not possible for other classes to inherit from them.

Knowledge round-up quiz

Next:  Expressions

 

Save My Place Glossary Language Reference Manual
Tutorials Overview Main site   
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice.