[fpc-pascal] Initial support for Custom Attributes

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Sat Jul 13 21:51:43 CEST 2019


On 12/07/2019 11:09 pm, Sven Barth via fpc-pascal wrote:
> type
>    TMyAttribute = class(TCustomAttribute)
>      constructor Create;
>      constructor Create(aArg: String);
>      constructor Create(aArg: TGUID);
>      constructor Create(aArg: LongInt);
>    end;
> 
>    {$M+}
>    [TMyAttribute]
>    TTestClass = class
>    private
>      fTest: LongInt;
>    published
>      [TMyAttribute('Test')]
>      property Test: LongInt read fTest;
>    end;


Nothing to do with the actual implementation of Custom Attributes, but
rather about using it.

I know attributes are TCustomAttribute descendants, but for the sake of
code readability, my personal opinion would be to NOT name custom
attributes with the 'T' prefix like is normally done in Object Pascal.

A visual comparison. So imagine a RESTful class could be decorated as
follows:


[Path('/troopers')]
[Produces('application/json')]
TStormtrooperResource = class(TObject)
  published
    [Path('/{id}')]
    [GET]
    function GetStormtrooper([PathParam('id')] id: string): TStormtrooper;
    [POST]
    function CreateTrooper(trooper: TStormTrooper): TStormtrooper;
end;


... instead of the traditional Object Pascal naming conversion....


[TPath('/troopers')]
[TProduces('application/json')]
TStormtrooperResource = class(TObject)
  published
    [TPath('/{id}')]
    [TGET]
    function GetStormtrooper([TPathParam('id')] id: string): TStormtrooper;
    [TPOST]
    function CreateTrooper(trooper: TStormTrooper): TStormtrooper;
end;


Just my observation..... Your mileage may vary. ;-)


Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp


More information about the fpc-pascal mailing list