Partial classes

Top  Previous  Next

Partial classes

Partial classes allow splitting the definition and implementation of a class over multiple files, or over multiple sections of the same file. Partial declarations must have the same modifiers (abstract, sealed, …), must specify the same ancestor (or none), however each partial declaration is allowed to introduce new interfaces and their implementations. 

Partial classes can be declared as class partial  (TAncestor) as well as with the Oxygene partial class  (TAncestor) syntax. 

 

Partial classes is yet a feature that has not made it into the Object Pascal standard. It basically means that the full declaration and implementation of a class or record type can be spread over multiple source-files. You can also pre-compile one part while you leave the other part open for implementation by others. This is the case for Mono C# under iOS for instance, where the pre-compiled application controller is expected to be completed by the framework user.

 

Partial classes is a very helpful feature when deployed with care. It is especially handy for large class hierarchies which target different models. Depending on the model set by the programmer- the implementation of certain features will differ according to the source-files included.

 

Smart Pascal supports two declaration formats of partial classes. The RemObjects Oxygene Pascal syntax, and also the more intuitive "type mytype = partial class(ancestor type)" variation. Partial classes are used throughout the Smart Pascal run-time library:

 

  TW3CustomControl = partial class(TW3MovableControl)
  private
    FAngle: Float;
    FScrollInfo: TW3ScrollInfo;
    FClassNames: TW3CSSClassStyleNames;
    FFont: TW3ControlFont;