[fpc-pascal] Feature Announcement: Support for multiple active helpers per type
Sven Barth
pascaldragon at googlemail.com
Fri May 10 21:56:56 CEST 2019
Hello together!
We are pleased to announce that Free Pascal now supports the usage of
multiple active helper types per extended type. This feature has been
developed by Ryan Joseph, so thank you very much Ryan.
To enable this feature you need to use the new modeswitch multihelpers.
This will then result in the following code to compile:
=== code begin ===
{$mode objfpc}
{$modeswitch typehelpers}
{$modeswitch multihelpers}
type
TObjectHelper1 = type helper for TObject
procedure Foo;
procedure Foobar;
end;
TObjectHelper2 = type helper for TObject
procedure Bar;
procedure Foobar;
end;
procedure TObjectHelper1.Foo;
begin
Writeln('Foo');
end;
procedure TObjectHelper1.Foobar;
begin
Writeln('Foobar1');
end;
procedure TObjectHelper2.Bar;
begin
Writeln('Bar');
end;
procedure TObjectHelper2.Foobar;
begin
Writeln('Foobar2');
end;
var
o: TObject;
begin
o.Foo;
o.Bar;
o.Foobar; // this will call TObjectHelper2.Foobar
end.
=== code end ===
As can be seen in the example above if a member is present in multiple
helpers then it will be resolved by the usual scoping rules. More common
is however is to have helpers in different units:
=== code begin ===
unit test1;
{$mode objfpc}
{$modeswitch typehelpers}
interface
type
TObjectHelper1 = type helper for TObject
function Func: LongInt;
end;
implementation
function TObjectHelper1.Func: LongInt;
begin
Result := 1;
end;
end.
unit test2;
{$mode objfpc}
{$modeswitch typehelpers}
interface
type
TObjectHelper2 = type helper for TObject
function Func: LongInt;
end;
implementation
function TObjectHelper2.Func: LongInt;
begin
Result := 2;
end;
end.
program testA;
uses
test1, test2;
var
o: TObject;
begin
Writeln(o.Func); // will print 2
end.
program testB;
uses
test2, test1;
var
o: TObject;
begin
Writeln(o.Func); // will print 1
end.
=== code end ===
Sidenote: yes, modeswitch typehelpers also allows "type helper" to be
used with classes and records.
Regards,
Sven
More information about the fpc-pascal
mailing list