Sample code in the documentation of unit TypInfo hardly usable
I never can remember the syntax for the function GetEnumName which allows to convert an enumeration to a string representation. Today I tried the sample code shown in the official documentation: https://www.freepascal.org/docs-html/current/rtl/typinfo/getenumname.html
program example9;
{ This program demonstrates the GetEnumName, GetEnumValue functions }
{$mode objfpc}
uses rttiobj,typinfo;
Var
O : TMyTestObject;
TI : PTypeInfo;
begin
O:=TMyTestObject.Create;
TI:=GetPropInfo(O,'MyEnumField')^.PropType;
Writeln('GetEnumName : ',GetEnumName(TI,Ord(O.MyEnumField)));
Writeln('GetEnumValue(mefirst) : ',GetEnumName(TI,GetEnumValue(TI,'mefirst')));
O.Free;
end.
I copied this code into Lazarus and tried to compile: Unit rttiobj not found...
After some searching I saw a note in the root page of the typinfo documentation saying that "the examples in this chapter use a rttiobj auxiliary unit, which contains an object that has a published property for all supported types. It also contains some auxiliary routines and definitions. This unit is included in the documentation sources, in the directory typinfex."
Seriously? I need to search for a unit in the documentation sources to run this simple documentation code? I don't even know where the documentation sources are...
There are lots of enumerations in the rtl and fcl. Why not use one of them in the example? Here is a modified version of the sample code which the user can apply immediately:
program example9;
{ This program demonstrates the GetEnumName, GetEnumValue functions }
{$mode objfpc}
uses
Classes, typinfo;
var
al: TAlignment; // TAlignment is declared in classes as (taLeftJustify, taRightJustify, taCenter)
begin
al := taLeftJustify;
WriteLn(GetEnumName(TypeInfo(TAlignment), ord(al)));
WriteLn(GetEnumValue(TypeInfo(TAlignment), 'taLeftJustify'));
end.
Note: all the documentation pages of the typinfo unit suffer from this issue.