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

Mailing lists / fpc-pascal / 2017-08
22 messages · 4 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
I just wanted to point out that revision 37023 Sven added type helper support for interfaces.
Good job and thank you Sven!
"Anthony Walter" wroteI 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
I just wanted to point out that revision 37023 Sven added type helper support for…
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.
completeness).
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
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
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')
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;Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
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
Here is another example: type TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday,…
I didn't know that helpers could be used in enums too. Great.
Best regards,
Marcos Douglas
I understood. In fact, this is a great feature for whose work with interfaces a lot. We…
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.
Interface helpers are only supported by FPC. But the other helpers (class, record, primitive types) are Delphi compatible.
Regards,
Sven
Helpers for primitive types are supported since 3.0.0.
Regards,
Sven
Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
"Anthony Walter" wroteMarco, 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
methods instances type. Interface helpers are only supported by FPC. But the other…
Alright, thanks for the info.
and/or properties. When you declare a type helper you extend all instances of said type…
Sven, multiple helpers would be greatly appreciated, especially considering everyone wants to add their own helpers for types like string.
Thanks again!
Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
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
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.
string);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
Sven, multiple helpers would be greatly appreciated, especially considering everyone…
"Anthony Walter" wroteSven, 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
Marco, it doesn't work that way. Type helpers simply allow you to extend an existing type…
And I just noticed: it's "type helper" if you want to extend an interface, not "record helper" ;)
Regards,
Sven
methods instances type. string); interface... I don't know about Java, but the C#…
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
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
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