[fpc-pascal] Enum RTTI

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Nov 5 11:27:46 CET 2009


On 05 Nov 2009, at 11:10, Wolfram Kl├Ąger wrote:

> maybe you want to try:
>
> uses
>  typinfo;
>
> // implementation
>
> function Str(const AEnum : TEnum) : String; overload;
> begin
>  Result := GetEnumName(TypeInfo(TEnum), ord(AEnum));
> end;
>
> I usually declare one such overload per enum type. If you are using  
> sysutils.Str, you have to avoid conflicts by chosing a different  
> function name, e.g. EnumStr.

Actually, as of FPC 2.4.0 str() is also supported for enum types (so  
in this case you don't need the writestr I mentioned earlier). It  
crashes if you pass an invalid enum value though, and there are next  
to no tests for this functionality so it may also contain other bugs.


Jonas


More information about the fpc-pascal mailing list