Check In Interface Type Helpers

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

Check In Interface Type Helpers

Anthony Walter-3
I just wanted to point out that revision 37023 Sven added type helper support for interfaces.

Good job and thank you 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: Check In Interface Type Helpers

Free Pascal - General mailing list

Am 23.08.2017 02:04 schrieb "Anthony Walter" <[hidden email]>:
>
> I just wanted to point out that revision 37023 Sven added type helper support for interfaces.
>
> Good job and thank you Sven!

You're welcome. I wanted to add them for some time already :)

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: Check In Interface Type Helpers

Michael Van Canneyt
In reply to this post by Anthony Walter-3


On Tue, 22 Aug 2017, Anthony Walter wrote:

> I just wanted to point out that revision 37023 Sven added type helper
> support for interfaces.
>
> Good job and thank you Sven!
>

I'm having trouble understanding why this could be useful (apart from completeness).
The only thing I can come up with is adding properties:

   Property SomeProp : TSomeType Read GetSomeProp Write SetSomeProp;

where GetSomeProp and SetSomeProp are part of the interface.

Any other examples ?

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

Re: Check In Interface Type Helpers

Free Pascal - General mailing list

Am 23.08.2017 09:42 schrieb "Michael Van Canneyt" <[hidden email]>:
>
>
>
> On Tue, 22 Aug 2017, Anthony Walter wrote:
>
>> I just wanted to point out that revision 37023 Sven added type helper
>> support for interfaces.
>>
>> Good job and thank you Sven!
>>
>
> I'm having trouble understanding why this could be useful (apart from completeness).
> The only thing I can come up with is adding properties:
>
>   Property SomeProp : TSomeType Read GetSomeProp Write SetSomeProp;
>
> where GetSomeProp and SetSomeProp are part of the interface.
>
> Any other examples ?

It's the same as for the other kinds of helpers: being syntactic sugar.
E.g. if I have an interface that has two methods that both return a string, but more often than not I need a combination of both results. The previous solution would have been a global function, now I can also do a helper with a method.

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: Check In Interface Type Helpers

Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 6:18 AM, Sven Barth via fpc-pascal
<[hidden email]> wrote:

> Am 23.08.2017 09:42 schrieb "Michael Van Canneyt" <[hidden email]>:
>>
>>
>> Any other examples ?
>
> It's the same as for the other kinds of helpers: being syntactic sugar.
> E.g. if I have an interface that has two methods that both return a string,
> but more often than not I need a combination of both results. The previous
> solution would have been a global function, now I can also do a helper with
> a method.

If I understood right, I could have a Interface with 1 method and
maybe 3 classes that implements this method. But one of theses classes
could use an interface helper to add 1 or more methods. All instances
(variables) will be interface-typed but in that last case, the
instance will have more that 1 original method.

Am I right?
(please say yes)

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

Re: Check In Interface Type Helpers

Anthony Walter-3
Marco, it doesn't work that way.

Type helpers simply allow you to extend an existing type with new methods and/or properties. When you declare a type helper you extend all instances of said type given that:

A) You 'use' the unit declaring the type helper in some other unit.
B) No other unit you're using also defines a type helper for that same type. Only one type helper per type allowed.

As to what they are useful for, consider the following:

type
  // IShellLink is define by Microsoft
  IShellLinkHelper = record helper for IShellLink
  public
    procedure Save(const Target, Description, Link: string);
  end;

procedure IShellLinkHelper.Save(const Target, Description, Link: string);
var
  P: IPersistsFile;
begin
  SetPath(Target); 
  SetDescription(Description);
  if Self is IPersistsFile then
  begin
    P := Self as IPersistsFile;
    P.Save(Link, True);
  end;
end;

...

CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, Link);

Link.Save('C:\Program Files\Google\chrome.exe', 'Web Browser', C:\Users\me\Desktop\The Internet.lnk')

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

Re: Check In Interface Type Helpers

Anthony Walter-3
Here is another example:

type 
  TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

  TDayHelper = record helper for TDay
    function AsByte: Byte;
    function ToString: string;
  end;

function TDayHelper.AsByte: Byte;
begin
  Result := Ord(Self);
end;

function TDayHelper.ToString: string;
begin
  case Self of
    Monday: Result := 'Monday';
    Tuesday: Result := 'Tuesday';
    Wednesday: Result := 'Wednesday';
    Thursday: Result := 'Thursday';
    Friday: Result := 'Friday';
    Saturday: Result := 'Saturday';
    Sunday: Result := 'Sunday';
  end;
end;

And you can have code like this:

var
  Day: TDay;
  S: string;
begin
  Day := TDay.Monday;
  S := Day.ToString.ToLower;
  WriteLn(S);
end;

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

Re: Check In Interface Type Helpers

Marcos Douglas B. Santos
In reply to this post by Anthony Walter-3
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter <[hidden email]> wrote:

> Marcos, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods
> and/or properties. When you declare a type helper you extend all instances
> of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same type.
> Only one type helper per type allowed.
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
>     procedure Save(const Target, Description, Link: string);
>   end;

Anthony,

I understood. In fact, this is a great feature for whose work with
interfaces a lot.
We can have small interfaces but add some methods in just some places,
extending such interfaces.

Is this compatible with Delphi?

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

Re: Check In Interface Type Helpers

Marcos Douglas B. Santos
In reply to this post by Anthony Walter-3
On Wed, Aug 23, 2017 at 11:07 AM, Anthony Walter <[hidden email]> wrote:

> Here is another example:
>
> type
>   TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday,
> Sunday);
>
>   TDayHelper = record helper for TDay
>     function AsByte: Byte;
>     function ToString: string;
>   end;

I didn't know that helpers could be used in enums too. Great.

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

Re: Check In Interface Type Helpers

Free Pascal - General mailing list
In reply to this post by Marcos Douglas B. Santos

Am 23.08.2017 16:58 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>
> On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter <[hidden email]> wrote:
> > Marcos, it doesn't work that way.
> >
> > Type helpers simply allow you to extend an existing type with new methods
> > and/or properties. When you declare a type helper you extend all instances
> > of said type given that:
> >
> > A) You 'use' the unit declaring the type helper in some other unit.
> > B) No other unit you're using also defines a type helper for that same type.
> > Only one type helper per type allowed.
> >
> > As to what they are useful for, consider the following:
> >
> > type
> >   // IShellLink is define by Microsoft
> >   IShellLinkHelper = record helper for IShellLink
> >   public
> >     procedure Save(const Target, Description, Link: string);
> >   end;
>
> Anthony,
>
> I understood. In fact, this is a great feature for whose work with
> interfaces a lot.
> We can have small interfaces but add some methods in just some places,
> extending such interfaces.
>
> Is this compatible with Delphi?

Interface helpers are only supported by FPC. But the other helpers (class, record, primitive types) are Delphi compatible.

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: Check In Interface Type Helpers

Free Pascal - General mailing list
In reply to this post by Marcos Douglas B. Santos

Am 23.08.2017 16:59 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>
> On Wed, Aug 23, 2017 at 11:07 AM, Anthony Walter <[hidden email]> wrote:
> > Here is another example:
> >
> > type
> >   TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday,
> > Sunday);
> >
> >   TDayHelper = record helper for TDay
> >     function AsByte: Byte;
> >     function ToString: string;
> >   end;
>
> I didn't know that helpers could be used in enums too. Great.

Helpers for primitive types are supported since 3.0.0.

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: Check In Interface Type Helpers

Free Pascal - General mailing list
In reply to this post by Anthony Walter-3

Am 23.08.2017 16:01 schrieb "Anthony Walter" <[hidden email]>:
>
> Marco, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods and/or properties. When you declare a type helper you extend all instances of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same type. Only one type helper per type allowed.
>

To be more precise: the last helper in scope simply wins. There is no error if multiple ones are in scope. Also one can cheat a bit by having one's own helper inherit from another helper of the same type ;)
Though I should mention that it's on my list to implement support for multiple helpers. I just need to be sure about the resolution rules :)

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: Check In Interface Type Helpers

Marcos Douglas B. Santos
In reply to this post by Free Pascal - General mailing list
On Wed, Aug 23, 2017 at 12:13 PM, Sven Barth via fpc-pascal
<[hidden email]> wrote:
> Am 23.08.2017 16:58 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>>
>> Is this compatible with Delphi?
>
> Interface helpers are only supported by FPC. But the other helpers (class,
> record, primitive types) are Delphi compatible.

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

Re: Check In Interface Type Helpers

Anthony Walter-3
In reply to this post by Free Pascal - General mailing list
Sven, multiple helpers would be greatly appreciated, especially considering everyone wants to add their own helpers for types like string.

Thanks again!

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

Re: Check In Interface Type Helpers

Marcos Douglas B. Santos
In reply to this post by Anthony Walter-3
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter <[hidden email]> wrote:

> Marco, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods
> and/or properties. When you declare a type helper you extend all instances
> of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same type.
> Only one type helper per type allowed.
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
>     procedure Save(const Target, Description, Link: string);
>   end;
>
> procedure IShellLinkHelper.Save(const Target, Description, Link: string);
> var
>   P: IPersistsFile;
> begin
>   SetPath(Target);
>   SetDescription(Description);
>   if Self is IPersistsFile then
>   begin
>     P := Self as IPersistsFile;
>     P.Save(Link, True);
>   end;
> end;

Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
real implementation not by a class, but by a "interface helper".
It's like default methods in Java(?) that we have code inside an interface...


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

Re: Check In Interface Type Helpers

Free Pascal - General mailing list

Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>
> On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter <[hidden email]> wrote:
> > Marco, it doesn't work that way.
> >
> > Type helpers simply allow you to extend an existing type with new methods
> > and/or properties. When you declare a type helper you extend all instances
> > of said type given that:
> >
> > A) You 'use' the unit declaring the type helper in some other unit.
> > B) No other unit you're using also defines a type helper for that same type.
> > Only one type helper per type allowed.
> >
> > As to what they are useful for, consider the following:
> >
> > type
> >   // IShellLink is define by Microsoft
> >   IShellLinkHelper = record helper for IShellLink
> >   public
> >     procedure Save(const Target, Description, Link: string);
> >   end;
> >
> > procedure IShellLinkHelper.Save(const Target, Description, Link: string);
> > var
> >   P: IPersistsFile;
> > begin
> >   SetPath(Target);
> >   SetDescription(Description);
> >   if Self is IPersistsFile then
> >   begin
> >     P := Self as IPersistsFile;
> >     P.Save(Link, True);
> >   end;
> > end;
>
> Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
> real implementation not by a class, but by a "interface helper".
> It's like default methods in Java(?) that we have code inside an interface...

I don't know about Java, but the C# equivalent would be extension methods. And yes, that's the point: the implementation is not part of the interface implementer, but the user of the interface can add some implementation. (Same for class, record and primitive types helpers)

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: Check In Interface Type Helpers

Free Pascal - General mailing list
In reply to this post by Anthony Walter-3

Am 23.08.2017 19:40 schrieb "Anthony Walter" <[hidden email]>:
>
> Sven, multiple helpers would be greatly appreciated, especially considering everyone wants to add their own helpers for types like string.

At least in FPC modes they can use inheritance to solve this (see below) or by putting their units after the SysUtils unit (at least if they don't want/need the helper provided in the SysUtils unit).

=== code begin ===

type
  TMyStringHelper = type helper(TStringHelper) for String
    // whatever
  end;

=== code end ===

Note: Delphi supports inheritance only for class helpers, but FPC supports it also for record and type helpers.

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: Check In Interface Type Helpers

Free Pascal - General mailing list
In reply to this post by Anthony Walter-3

Am 23.08.2017 16:01 schrieb "Anthony Walter" <[hidden email]>:
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
>     procedure Save(const Target, Description, Link: string);
>   end;
>
> procedure IShellLinkHelper.Save(const Target, Description, Link: string);
> var
>   P: IPersistsFile;
> begin
>   SetPath(Target); 
>   SetDescription(Description);
>   if Self is IPersistsFile then
>   begin
>     P := Self as IPersistsFile;
>     P.Save(Link, True);
>   end;
> end;
>

And I just noticed: it's "type helper" if you want to extend an interface, not "record helper" ;)

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: Check In Interface Type Helpers

Marcos Douglas B. Santos
In reply to this post by Free Pascal - General mailing list
On Wed, Aug 23, 2017 at 2:45 PM, Sven Barth via fpc-pascal
<[hidden email]> wrote:

> Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>>
>> Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
>> real implementation not by a class, but by a "interface helper".
>> It's like default methods in Java(?) that we have code inside an
>> interface...
>
> I don't know about Java, but the C# equivalent would be extension methods.
> And yes, that's the point: the implementation is not part of the interface
> implementer, but the user of the interface can add some implementation.
> (Same for class, record and primitive types helpers)

Would we have any problem of memory leaks using Interface helper with
COM interfaces (refcount)?
I mean, the "type helper" has constructor/destructor to create/release
something?

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

Re: Check In Interface Type Helpers

Free Pascal - General mailing list
On 23.08.2017 19:57, Marcos Douglas B. Santos wrote:

> On Wed, Aug 23, 2017 at 2:45 PM, Sven Barth via fpc-pascal
> <[hidden email]> wrote:
>> Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" <[hidden email]>:
>>>
>>> Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
>>> real implementation not by a class, but by a "interface helper".
>>> It's like default methods in Java(?) that we have code inside an
>>> interface...
>>
>> I don't know about Java, but the C# equivalent would be extension methods.
>> And yes, that's the point: the implementation is not part of the interface
>> implementer, but the user of the interface can add some implementation.
>> (Same for class, record and primitive types helpers)
>
> Would we have any problem of memory leaks using Interface helper with
> COM interfaces (refcount)?
> I mean, the "type helper" has constructor/destructor to create/release
> something?

It doesn't need to. A type helper is essentially syntactic sugar for a
class method with the extended parameter as first type.

Take this for example:

=== code begin ===

type
  TInterfaceHelper = type helper for IInterface
    procedure Something(aArg: String);
  end;

// is more or less equivalent to

  TInterfaceHelper = class
    class procedure Something(aExtended: IInterface; aArg: String);
  end;

=== code end ===

Sidenote: for primitive types and records the hidden Self parameter of
the helper is a "var" parameter as the extended type can be changed.

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