[fpc-pascal] generics class hierarchy

Sven Barth pascaldragon at googlemail.com
Sun Dec 19 13:49:55 CET 2010


On 19.12.2010 13:03, Honza wrote:
> 2010/12/19 Sven Barth<pascaldragon at googlemail.com>:
>>> "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 type parameter in the inheritance part clause, i.e. class(T) part
> of the generic class declaration has nothing to do with the
> placeholder formal parameter. The placeholder formal arg list are
> given inside the sharp brackets<>  as a kind of macro parameters. And
> the docs explicitly states that a placeholder identifier may not be
> used elsewhere in the generic declaration except for identifying the
> place where the "macro" expansion should substitute the
> instantiation/specialization parameter. So using it in the inheritance
> clause is invalid. IMO the docs are clear on this and my experiments
> seems to confirm this behaviour. I have excersised generics a lot to
> get heLib compiled and working. Still the latest changes in the
> compiler broke the published code as I realized very recently and not
> yet uploaded the remedy which sits on my local disk.
>

While I DO agree with you (after some thinking about the consequences) 
that a base class should not be allowed to be specified by a template 
parameter (and this is the way it already is), I don't agree with you 
that the documentation states this as clearly as you propose it.

The documentation says that every occurrence of a template parameter 
will be replaced by a type identifier (e.g. Integer, String, TObject). 
Now the documentation of a normal class declaration ( 
http://www.freepascal.org/docs-html/ref/refse31.html#x67-770006.1 ) 
states that the heritage clause contains a "class type identifier" which 
is just a special case of a type identifier.
So when one reads the documentation of generics one CAN (!) come to the 
(wrong) conclusion that you can also put a template parameter into the 
heritage clause of a class.

Here the documentation of generics should state more clearly that 
"class(T)" is not allowed, neither in the main class nor in sub classes.

>> I'm still thinking how David's idea could be achieved in another way which
>> is supported by the compiler...
>
> I've not yet got time to look at his goal at all, so I don't know. I
> just spotted the invalid constructs presented.
>

@David: Maybe you can restructure your class hierarchy to something like 
this (you'll need to be a bit creative here ^^):

   generic gt_box<_num> = class
     type
       t_point = specialize gt_point<_num>;
     var
       f_position: t_point;
       f_width, f_height : _num;
     end;

or this

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

I don't really see another possibility.

Regards,
Sven



More information about the fpc-pascal mailing list