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
TPhase = (Dark, Waxing, Full, Waning);declares four named values in order.case Phase ofbranches on the value itself, with one label per phase.Ord(Phase)gives its position, whilePredandSuccstep back and forward.
Keywords and builtins used here
OrdPredSuccWriteLnbegincaseendofprogramtypevar
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.
Step 1 of 3 in Enums & sets, step 16 of 27 in Language basics.