[fpc-pascal] Implicit generic specializations
Ryan Joseph
ryan at thealchemistguild.com
Sun Dec 2 10:13:15 CET 2018
Since I had was already working on generics I wanted to see if I could implement the inferred specializations like is available in Delphi. The current syntax is too verbose to be usable so this is a necessary improvement in my opinion.
Here’s a first draft which seems to be working. There’s probably other ways to do the inference (I don’t have Delphi to test there’s) but this what I did for now. The algorithm basically scans params in order of appearance and inserts unique non-repeating types. For example:
(1,'string') = <Integer,String>
(1,2,3,4,5,6) = <Integer>
('a','b') = <String>
('string',1) = <String,Integer>
('a',1,'b',2,'c') = <String,Integer>
https://github.com/genericptr/freepascal/tree/generic_implicit
{$mode objfpc}
{$modeswitch implicitgenerics}
program test;
generic procedure DoThis<T>(msg:T);
begin
writeln('DoThis$1#1:',msg);
end;
generic procedure DoThis<T>(msg:T;param1:T);
begin
writeln('DoThis$1#2:',msg,' ',param1);
end;
generic procedure DoThis<T,U>(msg:T);
begin
writeln('DoThis$2#1:',msg);
end;
generic procedure DoThis<T,U>(msg:T;param1:U);
begin
writeln('DoThis$2#2:',msg,' ',param1);
end;
generic procedure DoThis<T,U>(msg:T;param1:U;param2:tobject);
begin
writeln('DoThis$2#3:',msg,' ',param1,' ',param2.classname);
end;
begin
DoThis(1); // DoThis$1#1:1
DoThis(1,1); // DoThis$1#2:1 1
DoThis('a','a'); // DoThis$1#2:a a
DoThis('a',1); // DoThis$2#2:a 1
DoThis('a',1,tobject.create); // DoThis$2#3:a 1 TObject
end.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list