Sign in Join

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

Mailing lists / fpc-pascal / 2017-08

Check In Interface Type Helpers

22 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Anthony Walter

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

Good job and thank you Sven!

Reply by email

Sven Barth
to Anthony Walter
"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!

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

Regards,
Sven

Reply by email

Michael Van Canneyt
to Anthony Walter: I just wanted to point out that revision 37023 Sven added type helper support for…
Anthony Walter wrote 5 lines
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.

Reply by email

Sven Barth
to Michael Van Canneyt
"Michael Van Canneyt" wrote 12 lines
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).

Show quoted text 7 lines
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

Reply by email

Marcos Douglas B. Santos
to Sven Barth
Sven Barth wrote 10 lines
Am 23.08.2017 09:42 schrieb "Michael Van Canneyt" : > > > 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

Reply by email

Anthony Walter
to Marcos Douglas B. Santos

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')

Reply by email

Anthony Walter
to Anthony Walter

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;

Reply by email

Marcos Douglas B. Santos
to Anthony Walter: Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
Anthony Walter wrote 18 lines
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

Reply by email

Sven Barth
to Marcos Douglas B. Santos: I understood. In fact, this is a great feature for whose work with interfaces a lot. We…
"Marcos Douglas B. Santos" wrote 5 lines
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter 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.

Show quoted text 19 lines
> 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

Reply by email

Sven Barth
"Marcos Douglas B. Santos" wrote 14 lines
On Wed, Aug 23, 2017 at 11:07 AM, Anthony Walter 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

Reply by email

Sven Barth
to Anthony Walter: Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
"Anthony Walter" 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.

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

Reply by email

Marcos Douglas B. Santos
to Anthony Walter: Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
Anthony Walter wrote 31 lines
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

Reply by email

Sven Barth
to Marcos Douglas B. Santos
"Marcos Douglas B. Santos" wrote 5 lines
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter 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.

Show quoted text 12 lines
> 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);
Show quoted text 15 lines
> 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

Reply by email

Sven Barth
to Anthony Walter: Sven, multiple helpers would be greatly appreciated, especially considering everyone…
"Anthony Walter" wrote
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

Reply by email

Sven Barth
to Anthony Walter: Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
"Anthony Walter" wrote 23 lines
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

Reply by email

Marcos Douglas B. Santos
to Sven Barth: methods instances type. string); interface... I don't know about Java, but the C#…
Sven Barth wrote 11 lines
Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" : > > 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

Reply by email

Sven Barth
to Marcos Douglas B. Santos
Marcos Douglas B. Santos wrote 18 lines
On Wed, Aug 23, 2017 at 2:45 PM, Sven Barth via fpc-pascal wrote: > Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" : >> >> 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

Reply by email

Marcos Douglas B. Santos
to Sven Barth
Sven Barth wrote 8 lines
On 23.08.2017 19:57, Marcos Douglas B. Santos wrote: > 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.

So, is this work?

=== code begin ===

type
  IFoo = interface
    function Execute: IFoo;
  end;
  TFooHelper = type helper for IFoo
    procedure Something;
  end;
  TFoo = class(TInterfacedObject, IFoo)
  public
    function Execute: IFoo;
  end;
var
  F: IFoo;
begin
  F := TFoo.Create;
  F.Execute.Something;
end;

=== code end ===

Reply by email

Sven Barth
to Marcos Douglas B. Santos
Marcos Douglas B. Santos wrote 37 lines
On Wed, Aug 23, 2017 at 3:25 PM, Sven Barth via fpc-pascal wrote: > On 23.08.2017 19:57, Marcos Douglas B. Santos wrote: >> 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. So, is this work? === code begin === type IFoo = interface function Execute: IFoo; end; TFooHelper = type helper for IFoo procedure Something; end; TFoo = class(TInterfacedObject, IFoo) public function Execute: IFoo; end; var F: IFoo; begin F := TFoo.Create; F.Execute.Something; end; === code end ===

Yes, this should work.

Regards,
Sven

Reply by email