[fpc-pascal] generics class hierarchy

Sven Barth pascaldragon at googlemail.com
Sun Dec 19 11:43:25 CET 2010


On 19.12.2010 08:34, Honza wrote:
> 2010/12/19 David Emerson<dle3ab at angelbase.com>:
>> type
>>   generic gt_box<_t_point,_num>  = class (_t_point)   // FAILS :-(
>>    f_width, f_height : _num;
>>    end;
>
> I think it should fail according to the docs, see:
>
> http://www.freepascal.org/docs-html/ref/refse42.html
>
> "There is a single placeholder _T. It will be substituted by a type
> identifier when the generic class is specialized. The identifier _T
> *may not be used for anything else than a placehoder*. "
>

According to the documentation I'd say that it should succeed, because 
in "class(BaseClass)" "BaseClass" is a type identifier. But of course 
that has its own set of problems... see the second last paragraph of 
this mail.

> The bold part is IMO violated by the declaration. Anyway, it could be
> perhaps (not tested) written as:
>
> type
>    TBoxProxy = class(_t_point);
>    generic gt_box<_t_point, _num>  = class(TBoxProxy)
>      f_width, f_height : _num;
>    end;
>

No, this won't work, because "_t_point" won't be defined when 
"TBoxProxy" is parsed.

> Another strange point is, that the declaration of gt_box doesn't use
> the formal specialization paramater `_t_point` at all (in the posted
> code), so the same the other way around should also work:
>

It IS used, because David wants to influence the class the generic class 
gt_box inherits from when specializing the class.

E.g.:

type
   TIntPoint = class
     x, y: Integer;
   end;

   TFloatPoint = class
     x, y: Single;
   end;

   generic gt_box<_t_point, _num> = class(_t_point)
     width, height: _num;
   end;

   TFloatBox = specialize gt_box<TFloatPoint, Single>;
   TIntBox = specialize gt_box<TIntPoint, Integer>;

> type
>    generic gt_box<_num>  = class<_t_point>
>      f_width, f_height : _num;
>    end;
>

This won't compile because of the "<...>" around "_t_point". Also it's 
not what Daniel intends.

> A 3rd note is that your code can't compile as _t_point is not declared
> when gt_box is declared, but the declaration wants to inherit from
> _t_point, so IMO also for this the code is rightfully rejected the
> compiler.

The question is whether this should be rejected if "_t_point" is a 
template parameter... on the other hand this would violate compile time 
checks of the generic class...

I'm still thinking how David's idea could be achieved in another way 
which is supported by the compiler...

Regards,
Sven



More information about the fpc-pascal mailing list