[fpc-pascal] Unrelated type helpers with the same members? Implement an interface with a type helper?

Ondrej Pokorny lazarus at kluug.net
Fri Dec 23 16:47:17 CET 2022


On 23.12.2022 15:50, Andrew Haines via fpc-pascal wrote:
> On 12/23/22 5:24 AM, Ondrej Pokorny via fpc-pascal wrote:
>> This may be simpler:
>>
>> [TEnumAttr(['YES', 'NO', 'COULD_BE'])]
>>   TMyEnum = (meYes, meNo, meCouldBe);
>>
>> TEnumAttr = class(TCustomAttribute)
>>   protected
>>     FValues: TStringArray;
>>     FTypeName: String;
>>   public
>>     class function FromType(ATypeInfo: PTypeInfo): TEnumAttr;
>>     constructor Create(Values: array of string);
> I tried that first but the compiler gave an error on the "["
>
> Error: Ordinal expression expected
>
> [TEnumAttr(['YES', 'NO', 'COULD_BE'])]

Indeed. I would say that is a bug and therefore I reported it:
https://gitlab.com/freepascal.org/fpc/source/-/issues/40058

I usually use the scoped enums but in case the enum values are not valid 
identifiers this is the perfect way to handle them. Therefore thanks for 
the idea :) I use attributes for various things but didn't realize this 
possibility.

Ondrej



More information about the fpc-pascal mailing list