[fpc-pascal] implements

Sven Barth pascaldragon at googlemail.com
Sat Sep 2 09:35:49 CEST 2017


Am 02.09.2017 06:34 schrieb "Ryan Joseph" <ryan at thealchemistguild.com>:
>
> I think I asked this some years ago but I came across it again I just
don’t get what the point of this is. There is an “implements” property but
it seems like yet another half-baked feature from Delphi that wasn’t
implemented completely or is broken. What’s the point of implementing an
interface like this on TBaseClass if you need to access all the methods by
using the property name (“hook” in this case) when you could just add an
instance of THook in TBaseClass? It adds so much noise and clutter in the
language and for what? The only reason it makes sense is if you could call
“base.DoIt” and omit the .hook every time you’re typing but that was
overlooked for some reason. Why??
>
>
> type
>   IHook = interface ['IHook']
>     procedure DoIt;
>   end;
>
> type
>         THook = class (IHook)
>                 procedure DoIt;
>         end;
>
> procedure THook.DoIt;
> begin
>         writeln(ClassName+' did it');
> end;
>
> type
>         TBaseClass = class (IHook)
>                 private
>                         m_hook: IHook;
>                 public
>                         property Hook: IHook read m_hook implements IHook;
>                         constructor Create;
>         end;
>
> constructor TBaseClass.Create;
> begin
>         m_hook := THook.Create;
> end;
>
>
> base: TBaseClass;
>
> base := TBaseClass.Create;
>
> base.DoIt; // CANT DO THIS
> base.hook.DoIt; // MUST DO THIS

Because you must use the interface and not the class instance:

=== code begin ===

var
  base: TBaseClass;
  hook: IHook;
begin
  base := TBaseClass.Create;
  hook := base;

  hook.DoIt; // will call base.hook.DoIt
end;

=== code end ===

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170902/13c2cb04/attachment.html>


More information about the fpc-pascal mailing list