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

Sven Barth pascaldragon at googlemail.com
Sat Nov 21 19:12:42 CET 2015


On 21.11.2015 19:09, leledumbo wrote:
> Congratulations! Nice work, Sven.
> It's a bit weird to see the specialize in the middle of an expression but
> that's not a problem for me. The one that needs explanation is "complex
> expressions", what's the minimum requirement for an expression to be called
> complex for this case?

I'll simply quote my answer to Maciej on fpc-devel:

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-pascal mailing list