Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-05

Feature Announcement: Support for multiple active helpers per type

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sven Barth

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

Reply by email

silvioprog
to Sven Barth

On Fri, May 10, 2019 at 4:57 PM Sven Barth via fpc-pascal <

Show quoted text 8 lines
wrote: 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.

awesome news.

thanks for implementing it dudes, i'm going to update my git to test it!

Show quoted text 127 lines
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
Silvio Clécio

Reply by email

Mattias Gaertner
to Sven Barth: Hello together! We are pleased to announce that Free Pascal now supports the usage of…
[...] 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.

Thanks Ryan!

Show quoted text 22 lines
[...] 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.

Ehm, these two examples only demonstrates that the last helper wins. They do not demonstrate multihelpers, do they?

Mattias

Reply by email

Sven Barth
to Mattias Gaertner
Mattias Gaertner wrote 34 lines
On Fri, 10 May 2019 21:56:56 +0200 Sven Barth via fpc-pascal wrote: > [...] > 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. Thanks Ryan! > [...] > 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. Ehm, these two examples only demonstrates that the last helper wins. They do not demonstrate multihelpers, do they?

Well, if you'd add the methods from the first test you'd see that both helpers are available and that for Func the last helper wins...

Regards,
Sven

Reply by email

Anthony Walter
to Sven Barth

Wow Sven, thank you and also to the other team members who made this happen. This is a really big deal and IMO helps solve a lot of problems. For me personally I plan to implement this into a lot of projects.

I just finished rebuilding FPC 3.04 and Lazarus 2.0.2 as the main versions for myself and at my website, but I will definitely rebuild and test from svn sources soon.

Thanks again and that's some great work!

Reply by email