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

Sven Barth pascaldragon at googlemail.com
Sun Nov 22 10:32:10 CET 2015


On 22.11.2015 00:04, Anthony Walter wrote:
> Sven, first off, congratulations and we thank you for so much good work.
> in the future do you see a time when type inference could be used?
>
> S := Add('hello ', 'world');
> I := Add(1, 2);

I honestly don't think that type inference should become part of Pascal, 
it's simply not part of the language philosophy and it would also not 
play well with the need for "specialize" in ObjFPC mode and as long as 
Delphi doesn't introduce it I won't add it only for mode Delphi either.

> Which then brings to mind, will it be possible to get use references to
> generic routines?

If you have read the limitations part of my mail then you should know 
that this is a known limitation and to be addressed in the future.

> type
>    TTransform<T> = function<T>(const A, B: T): T;

FPC already supports this, though the syntax is

=== code begin ===

type
   TTransform<T> = function(const A, B: T): T;

=== code end ===

Or with added "generic" keyword in case of non-Delphi modes.

> var
>    Transform: TTransform<T>;

This is not a valid type declaration and never will be, because "T" is 
not defined. The correct one is

=== code begin ===

var
   Transform: TTransform<String>;

=== code end ===

> begin
>    Transform := Add<String>;
>    WriteLn(Transform('hello', 'world'));
>    Transform := Min<String>;
>    WriteLn(Transform('hello', 'world'));
>    Transform := Max<String>;
>    WriteLn(Transform('hello', 'world'));
> end;

This would work once assignments of routines work. In non-Delphi modes 
the syntax will be

=== code begin ===

   Transform := @specialize Add<String>;
   Writeln(Transform('hello', 'world'));

=== code end ===

> function Print<T>(const A, B: T; Transform: TTransform<T>);
> begin
>    WriteLn(Transform(A, B));
> end;
>
> begin
>    Print('hello', 'world', Add); // type inference
>    Print('hello', 'world', Min);
>    Print('hello', 'world', Max);
> end;

No type inference.

Regards,
Sven




More information about the fpc-devel mailing list