[fpc-pascal] Check In Interface Type Helpers

Sven Barth pascaldragon at googlemail.com
Wed Aug 23 19:45:56 CEST 2017


Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" <md at delfire.net>:
>
> On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter <sysrpl at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170823/7050c0b8/attachment.html>


More information about the fpc-pascal mailing list