[fpc-devel] Language extension: absolute for classes

Micha Nelissen micha at neli.hopto.org
Sat Sep 30 09:01:59 CEST 2006


Hi,

I want to bring up the following scenario: (need fixed font)

   B  -->  G
   |       |
   A  -->  F

All are classes, and usually A 'owns' F. So A has a field 'Field' of
type F. Now, whenever A creates F, B overrides this (in virtual method
or class type) to create a G.

The problem now is that every time B wants to access G, it has to
typecast Field to G.

I want to propose something like:

type
  B = class(A)
  protected
    Field: G absolute Field;
    ...
  end;

Whenever the implementation of methods of B (or descendants of B)
accesses Field, the compiler will use type G automatically, so that no
typecast is necessary.

The compiler option -Criot might be used to check whether the type is
actually the suggested type, in extension to the current behaviour where
class typecasts are converted to 'as' under usage of this switch.

I realize that this is only a 'saving-typing' language extension, but it
does save significant amounts of typing and would make the code more
readable (IMHO).

It would be useful in the LCL: TLCLComponent.FWidgetSetClass which is a
metaclasstype, and TWinControl (see wincontrol.inc:2053 for current
typecast example), TEdit etc. want to use a descendant metaclasstype.
It would also be useful in lNet: TLConnection has a FIterator which is a
TLSocket, but descendants can choose to create descendants of TLSocket
instead.

Opinions ?

Micha



More information about the fpc-devel mailing list