[fpc-pascal] generics class hierarchy

David Emerson dle3ab at angelbase.com
Sun Dec 19 12:16:44 CET 2010

On Sat 18 Dec 2010, 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*. "

Well, IMO the docs are a bit vague as to the definition of "placeholder". 
However, according to experience:
- a placeholder _can_ be used for a type identifier that is used as a field 
within the class
- a placeholder can't be used for a type identifier that is used to specify the 
ancestor class to inherit from

Maybe "placeholder" is also referring to something that's going on internally to 
the compiler -- when the generic is defined, its class structure and VMT and 
everything must be known. I'm no compiler writer, but I guess it makes sense 
from that perspective. I was thinking of "placeholder" from the other 
perspective, in terms of words: the specialization substitutes these words into 
those places, and those places can by any type identifiers. More like what I'd 
expect from e.g. a scripting language. But it doesn't work like that.

> 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 is completely not right at all. _t_point is a placeholder. I want to 
use a placeholder to specify the ancestor class. In your example here you're 
treating _t_point as an already-defined class which TBoxProxy can inherit from. 
_t_point doesn't exist, it never exists, it's just a placeholder for a proper 
type identifier.

> 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)

yes it does. It is used to specify the ancestor class.
But I think I have now beaten the issue to death.

I am curious, though, if e.g. delphi allows this syntax?


> , so the same the other way around should also work: 
> type
>   generic gt_box<_num> = class<_t_point>
>     f_width, f_height : _num;
>   end;
> 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.
> -- 
> bflm
> freepascal-bits.blogspot.com
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

More information about the fpc-pascal mailing list