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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With