[fpc-devel] Feature announcement: Generic functions, procedures and methods

Sven Barth pascaldragon at googlemail.com
Sat Nov 21 19:09:59 CET 2015


On 21.11.2015 18:46, Maciej Izak wrote:
>     Because of the missing "specialize" keyword that mark
>     specializations complex expressions *do not* work in mode Delphi
>     yet. So assignments of function results are okay, but other than
>     that you'll likely encounter compiler errors.
>
>
> Could you please show some example of limitations?

Just take the example that I posted and convert to mode Delphi:

=== code begin ===

{$mode delphi}

function Add<T>(aLeft, aRight: T): T;
begin
   Result := aLeft + aRight;
end;

begin
   Writeln(Add<String>('Generic ', 'routines') + Add<String>(' with ', 
'Free Pascal'));
end.

=== code end ===

The compiler will error out like this:

=== output begin ===

Target OS: Linux for i386
Compiling ./fpctests/tgenroutines.pp
tgenroutines.pp(9,47) Fatal: Syntax error, ")" expected but "+" found
Fatal: Compilation aborted

=== output end ===

What will work however is this:

=== example begin ===

{$mode delphi}

function Add<T>(aLeft, aRight: T): T;
begin
   Result := aLeft + aRight;
end;

var
   s1, s2: String;
begin
   s1 := Add<String>('Generic ', 'routines');
   s2 := Add<String>(' with ', 'Free Pascal');
   Writeln(s1, s2);
end.

=== example end ===

So basically just use it as a single factor (or statement) and you'll be 
fine until I've improved that.

Regards,
Sven



More information about the fpc-devel mailing list