Feature Announcement: Support for multiple active helpers per type

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Feature Announcement: Support for multiple active helpers per type

Free Pascal - General mailing list
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Feature Announcement: Support for multiple active helpers per type

silvioprog
On Fri, May 10, 2019 at 4:57 PM Sven Barth via fpc-pascal <[hidden email]> 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!
 
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Feature Announcement: Support for multiple active helpers per type

Free Pascal - General mailing list
In reply to this post by Free Pascal - General mailing list
On Fri, 10 May 2019 21:56:56 +0200
Sven Barth via fpc-pascal <[hidden email]> 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?

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Feature Announcement: Support for multiple active helpers per type

Free Pascal - General mailing list
Am 10.05.2019 um 22:21 schrieb Mattias Gaertner via fpc-pascal:

> On Fri, 10 May 2019 21:56:56 +0200
> Sven Barth via fpc-pascal <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Feature Announcement: Support for multiple active helpers per type

Anthony Walter-3
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! 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal