6.9 Nested types, constants and variables

A class definition can contain a type section, const section and a variable section. The type and constant sections act as a regular type section as found in a unit or method/function/procedure implementation. The variables act as regular fields of the class, unless they are in a class var section, in which case they act as if they were defined at the unit level, within the namespace of the class (section 6.3, page 316).

However, the visibility of these sections does play a role: private and protected (strict or not) constants, types and variables can only be used as far as their visibility allows.

Public types can be used outside the class, by their full name:

type
  TA = Class(TObject)
  Public
    Type TEnum = (a,b,c);
    Class Function DoSomething : TEnum;
  end;

Class Function TA.DoSomething : TEnum;

begin
  Result:=a;
end;

var
  E : TA.TEnum;

begin
  E:=TA.DoSomething;
end.

Whereas

type
  TA = Class(TObject)
  Strict Private
    Type TEnum = (a,b,c);
  Public
    Class Function DoSomething : TEnum;
  end;

Class Function TA.DoSomething : TEnum;

begin
  Result:=a;
end;

var
  E : TA.TEnum;

begin
  E:=TA.DoSomething;
end.

Will not compile and will return an error:

tt.pp(20,10) Error: identifier idents no member "TEnum"

Note that for writeable constants, the same rules apply as for class variables with regard to scope and overriding in descendents:

{$mode delphi}{$J+}
type
  TA = class // base type
    const CODE: integer = 99;
  end;
  TB = class(TA);
  TC = class(TA);

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

Will write

 2 2 2

But

{$mode delphi}{$J+}
type
  TA = class // base type
    const CODE: integer = -99;
  end;
  TB = class(TA)
    const code : integer = -98;
  end;
  TC = class(TA)
    Const code : integer = -97;
  end;

begin
  TA.Code:=0;
  TB.Code:=1;
  TC.Code:=2;
  Writeln(Ta.Code:2,Tb.Code:2,Tc.code:2);
end.

Will write

 0 1 2