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

Marcos Douglas md at delfire.net
Thu Jul 16 21:12:03 CEST 2015


On Thu, Jul 16, 2015 at 1:46 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> On 16.07.2015 17:53, Marcos Douglas wrote:
>>
>> [...]
>>
>> 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 ^^)

Ok Sven, you and others persuaded me to continue using the mode ObjFPC.
Thank you for your explanations.

Regards,
Marcos Douglas



More information about the fpc-pascal mailing list