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

Sven Barth pascaldragon at googlemail.com
Thu Jul 16 18:39:43 CEST 2015


On 16.07.2015 18:02, Marcos Douglas wrote:
> On Thu, Jul 16, 2015 at 11:44 AM, Michael Van Canneyt
> <michael at freepascal.org> wrote:
>>
>>
>> On Thu, 16 Jul 2015, Maciej Izak wrote:
>>
>>> sadly - no, only in Delphi mode. btw. this thing keep me away from objfpc.
>>
>>
>> That seems like a very strange reason to me.
>>
>> The fact that you must type 1 word in certain places keeps you from using an
>> otherwise useful mode ?
>> This word is there for clarity, It is meant to help you, to make explicit
>> you are in fact specializing a new type.
>
> Yes, but do you think this is more verbose unnecessarily? Because the
> syntax TFoo<T> (I mean this "<>") show us that is a generic, don't?

This might be the case for a human, but for a compiler/parser that is a 
completely different topic. Take the following example code (with 
generic methods to highlight the problems I'm facing right now):

=== code begin ===

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

var
   t: TTest;
   i: LongInt;
begin
   i := t.Add<LongInt>(2, 4);
end;

=== code end ===

The parser sees the following tokens after the assignment: identifier 
('t'), point, identifier ('Add'), less than, identifier ('LongInt'), 
greater than, left parenthesis, etc.
Our parser only knows the next token, up until the "greater than" (or if 
there is more than one type parameter the "," for the next parameter) 
the parser does not know whether it is dealing with a call to generic 
method Add<X, Y, Z> or a call to a function Add followed by a 
comparison. This is highly ambigous and a real PITA to implement.

In non-Delphi modes this is much more straight forward:

=== code begin ===

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

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


=== code end ===

No sambiguity here. Nice :)

Regards,
Sven



More information about the fpc-pascal mailing list