Jump to content
havrlisan

Using attributes on aliases

Recommended Posts

Is it possible to use attributes in these scenarios:

// First scenario
[MyAttr]
TMyGuid = TGuid;

// Second scenario
[MyAttr]
TMyDT = TDateTime;

I found this article that says it's not possible to use attributes on aliases of simple ordinal types, only on actual sub-types by using the type keyword. I'm asking this because I believe declaring my TMyGuid type as a sub-type for TGuid rather than an alias will break a lot of my code. Is there really no way to use attributes on aliases like this?

 

Update: I realized that changing my declaration to

TMyGuid = type TGuid;

will only cause the loss of TGuidHelper. Unfortunately, class helper inheritance is still not available so I will have to rewrite it for my sub-type.

Edited by havrlisan
update

Share this post


Link to post
type
  TMyGUID = TGUID; // your "alias" that way

  TMyHlpToTGUID = record helper for TGUID
    function MyFunc: string;
  end;

  TMyHlpToTMyGUID = record helper for TMyGUID
    function MyMyFunc: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x: TMyGUID;
begin
  x := TMyGUID.Create('123...');
  x.MyMyFunc; // MyMyFunc "hide" MyFunc now... helper to same type-base TGUID!
end;

{ TMyHlpToTGUID }

function TMyHlpToTGUID.MyFunc: string;
begin
  result := 'Hello';
end;

{ TMyHlpToTMyGUID }

function TMyHlpToTMyGUID.MyMyFunc: string;
begin
  result := 'World';
end;

end.

as "TMyGUID" is, in fact, "TGUID" (yet), then your attributes should works in "TGUID" not in "TMyGUID"

as showed in your "blog post", the real type is "TGUID", then, you needs works with it, not "TMyGUID"

 

image.thumb.png.03a6a1938dd765321eb31a36d7e7e1ff.png

Edited by programmerdelphi2k

Share this post


Link to post
On 12/9/2022 at 2:11 PM, programmerdelphi2k said:

type
  TMyGUID = TGUID; // your "alias" that way

  TMyHlpToTGUID = record helper for TGUID
    function MyFunc: string;
  end;

  TMyHlpToTMyGUID = record helper for TMyGUID
    function MyMyFunc: string;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x: TMyGUID;
begin
  x := TMyGUID.Create('123...');
  x.MyMyFunc; // MyMyFunc "hide" MyFunc now... helper to same type-base TGUID!
end;

{ TMyHlpToTGUID }

function TMyHlpToTGUID.MyFunc: string;
begin
  result := 'Hello';
end;

{ TMyHlpToTMyGUID }

function TMyHlpToTMyGUID.MyMyFunc: string;
begin
  result := 'World';
end;

end.

as "TMyGUID" is, in fact, "TGUID" (yet), then your attributes should works in "TGUID" not in "TMyGUID"

as showed in your "blog post", the real type is "TGUID", then, you needs works with it, not "TMyGUID"

 

image.thumb.png.03a6a1938dd765321eb31a36d7e7e1ff.png

I see, so it is not possible to declare attributes for aliases. Thanks for your answer.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×