Can I define a recording assistant for TBytes?

I am using Delphi XE4. I am trying to define some helper function for TBytes:

TBytesHelper = record helper for TBytes public function GetLength: integer; end; function TBytesHelper.GetLength: integer; begin Result := System.Length(Self); end; 

When I try to use a new helper function:

 var B: TBytes; i: integer; begin B := TBytes.Create(1,2,3); i := B.GetLength; if i <> Length(B) then raise Exception.Create('Incorrect result'); end; 

I except for the result for i is 3 , but it is not. I refer to the definition of TStringHelper in SysUtils.pas, which has a similar construct.

Is there something I missed?

+6
source share
1 answer

This issue was discussed here: https://forums.embarcadero.com/thread.jspa?threadID=88409 In a few words, you can define your own type and use it with the write assistant:

 type TMyBytes = array of Byte; TBytesHelper = record helper for TMyBytes function GetLength: integer; end; 

But it does not work with TBytes defined in Delphi. Helper support for generic types has recently been added, so this is probably a kind of limitation of the current implementation.

+2
source

Source: https://habr.com/ru/post/947013/


All Articles