For simple types, the rules are pretty much the same as for records, plus there are some extra
requirements:
- Support for type helpers needs to be activated using the modeswitch typehelpers:
{$modeswitch typehelpers}
This modeswitch is enabled by default only in mode Delphi and DelphiUnicode.
- In Delphi (and DelphiUnicode) mode, for stricter Delphi compatibility, the record helpers
must be used instead of a type helper.
- The modes ObjFPC and MacPas use type helper, but the modeswitch TypeHelpers must be
used.
- The following types are not supported:
This of course means that all other simple types are supported.
- Type helpers can implement constructors.
- Inheritance of record helpers is only allowed in ObjFPC mode; In Delphi mode, it is not
allowed.
- Type helpers can only descend from other type helpers, not from class or record
helpers.
- A descendent type helper must extend the same type.
The following gives an idea of the possibilities:
{$mode objpas}
{$modeswitch typehelpers}
type
TLongIntHelper = type helper for LongInt
constructor create(AValue: LongInt);
class procedure Test; static;
procedure DoPrint;
end;
constructor TLongIntHelper.create(AValue: LongInt);
begin
Self:=Avalue;
DoPrint;
end;
class procedure TLongIntHelper.Test;
begin
Writeln(’Test’);
end;
procedure TLongIntHelper.DoPrint;
begin
Writeln(’Value :’,Self);
end;
var
i: LongInt;
begin
I:=123;
i.Test;
$12345678.Test;
LongInt.Test;
I:=123;
i.DoPrint;
$12345678.DoPrint;
end.