[fpc-pascal] Interface syntax: Is possible don't use specialize in mode objfpc?

Sven Barth pascaldragon at googlemail.com
Thu Jul 16 18:46:52 CEST 2015


On 16.07.2015 17:53, Marcos Douglas wrote:
> On Thu, Jul 16, 2015 at 12:23 PM, Sven Barth
> <pascaldragon at googlemail.com> wrote:
>> Am 16.07.2015 16:34 schrieb "Maciej Izak" <hnb.code at gmail.com>:
>>>
>>> sadly - no, only in Delphi mode. btw. this thing keep me away from objfpc.
>>>
>>
>> Then let me tell you that generic methods will arrive in mode ObjFPC (and
>> thus its syntax) first, because the Delphi syntax is a PITA to parse/handle.
>> (in fact generic methods are already working in mode ObjFPC in my local
>> repo, I just want to get the Delphi ones working a little bit before
>> commiting to trunk)
>
> I didn't understand. This already works... or you talking about "work
> with the same syntax than Delphi in mode objfpc"?

Generic methods are not yet supported by FPC. Try the following Delphi 
code in 3.1.1 for example (in mode Delphi):

=== code begin ===

type
   TTest = class
     function Add<T>(aLeft, aRight: T): T;
   end;

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

var
   t: TTest;
   i: LongInt;
   s: String;
begin
   i := t.Add<LongInt>(2, 4);
   s := t.Add<String>('Hello', 'World');
end.

=== code end ===

This does not compile in FPC currently. In my local repo the above is 
close to working and the following non-Delphi-mode code definitely works:

=== code begin ===

type
   TTest = class
     generic function Add<T>(aLeft, aRight: T): T;
   end;

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

var
   t: TTest;
   i: LongInt;
   s: String;
begin
   i := t.specialize Add<LongInt>(2, 4);
   s := t.specialize Add<String>('Hello', 'World');
end.

=== code end ===

>
> I will repeat also the last paragraph of the first message:
> I would like to know why exists this difference, what the advantages
> for mode objfpc using this syntax.

Less ambiguity for both the user as well as the compiler. In fact if I 
had already been with a FPC developer back when generics were 
implemented before 2.2 I had implemented specialization somewhat like 
the following:

=== code begin ===

begin
   i := t.specialize Add with (LongInt)(2,4);
   i := t.specialize Add with (String)('Hello', 'World');
end.

=== code end ===

This way there would be no ambiguity with the "<" at all... but sadly I 
wasn't a FPC developer back then. :) (and I would have needed to 
implement the Delphi compatible one for mode Delphi anyway and just like 
I do now I would have cursed just as much ^^)

Regards,
Sven



More information about the fpc-pascal mailing list