typestar

Enumerated types in Delphi

TPhase names the four moon phases, and a case picks the line to print.

program EnumsBasics;
type
  TPhase = (Dark, Waxing, Full, Waning);
var
  Phase: TPhase;
begin
  Phase := Full;
  case Phase of              // case branches on the value, not on a number
    Dark: WriteLn('no moon tonight');
    Waxing: WriteLn('growing');
    Full: WriteLn('full moon');
    Waning: WriteLn('shrinking');
  end;
  // Ord gives the position in the list, counting from zero.
  WriteLn('ord ', Ord(Phase), ', prev ', Ord(Pred(Phase)));
  WriteLn('next phase ', Ord(Succ(Phase)));   // Succ steps forward one
end.

How it works

  1. TPhase = (Dark, Waxing, Full, Waning); declares four named values in order.
  2. case Phase of branches on the value itself, with one label per phase.
  3. Ord(Phase) gives its position, while Pred and Succ step back and forward.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
495
Tokens
108
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 86 seconds.

Type this snippet

Step 1 of 3 in Enums & sets, step 16 of 27 in Language basics.

← Previous Next →