[fpc-devel] Thoughts on being able to declare "pointer-to-type" parameters directly in method signatures?

denisgolovan denisgolovan at yandex.ru
Mon Jun 10 09:09:50 CEST 2019


> You can do it like this (yes, works in mode Delphi as well):
> 
> === code begin ===
> 
> {$mode objfpc}
> {$modeswitch advancedrecords}
> 
> type
> generic TPointerType<T> = record
> public type
> PT = ^T;
> end;
> 
> generic procedure MemCopy<T>(Dest, Src: specialize
> TPointerType<T>.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: specialize TArray<String4> = ('AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE');
> Y: specialize TArray<String4> = ('', '', '', '', '');
> S: String4;
> 
> begin
> specialize MemCopy<String4>(@Y[0], @X[0], 5);
> for S in Y do WriteLn(S);
> end.
> 
> === code end ===
> 
> Regards,
> Sven

Maybe we can get generic aliases instead?
I mean 

type
  generic TPtr<T> = ^T;

... then put TPtr into some FPC system unit and at least provide one single common type instead of re-inventing it in each separate library?

It'll be at least something like:

  generic function Test<T>( ptr: TPtr<T>);

I mean we already have TArray<T>, right?

-- 
Regards,
Denis Golovan



More information about the fpc-devel mailing list