Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define record helper for TBytes?

I am using Delphi XE4. I try 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 consume the 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 the result for i is 3 but it doesn't. I refer to TStringHelper define in SysUtils.pas that has similar construct.

Is there anything I miss?

like image 610
Chau Chee Yang Avatar asked Jun 11 '13 08:06

Chau Chee Yang


1 Answers

This problem was discussed here: https://forums.embarcadero.com/thread.jspa?threadID=88409 In few words - you can define your own type and use it with record helper:

type
 TMyBytes = array of Byte;

 TBytesHelper = record helper for TMyBytes
   function GetLength: integer;
 end; 

But it doesn't work with TBytes defined in Delphi. Support of helpers for generic types was added recently, so probably it is kind of limitations of current implementation.

like image 160
Andrei Galatyn Avatar answered Oct 17 '22 22:10

Andrei Galatyn