[fpc-devel] Thoughts on being able to declare "pointer-to-type" parameters directly in method signatures?
Ben Grasset
operator97 at gmail.com
Fri Jun 7 18:37:21 CEST 2019
For example, consider the following code, that implements a generic
"memcpy"-style procedure using a static advanced-record method:
program Example;
{$mode Delphi}
type
TMem<T> = record
public type
PT = ^T;
public
class procedure Copy(Dest: PT; Src: PT; Len: PtrUInt); static; inline;
end;
class procedure TMem<T>.Copy(Dest: PT; Src: PT; Len: PtrUInt);
begin
while Len > 0 do begin
Dest^ := Src^;
Inc(Dest);
Inc(Src);
Dec(Len);
end;
end;
type
String4 = String[4];
var
X: TArray<String4> = ['AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE'];
Y: TArray<String4> = ['', '', '', '', ''];
S: String4;
begin
TMem<String4>.Copy(@Y[0], @X[0], 5);
for S in Y do WriteLn(S);
end.
The only reason the record is necessary at all is to be able to declare
"PT" as a pointer type alias to the record's overall "T".
Personally, I'd much rather simply write the following, if it was possible:
procedure MemCopy<T>(Dest: ^T; Src: ^T; Len: PtrUInt);
begin
while Len > 0 do begin
Dest^ := Src^;
Inc(Dest);
Inc(Src);
Dec(Len);
end;
end;
Of course, currently that's not valid code. This kind of thing is useful
for more than just generics, also: it would be just as applicable to
non-generic records, primitive types, e.t.c.
Surely I can't be the only one who has ever wanted to use a "typed pointer"
without first having to declare a named alias.
So I guess my questions are:
A) How feasible is this to implement?
B) Is there a specific reason is has *not* been implemented previously?
(Seems unlikely)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190607/956af7a5/attachment.html>
More information about the fpc-devel
mailing list