[fpc-pascal] Interface delegates and the implements property specifier

Ryan Joseph genericptr at gmail.com
Fri Dec 27 22:53:23 CET 2019



> On Dec 27, 2019, at 1:39 PM, Adriaan van Os <fpc at microbizz.nl> wrote:
> 
> etcetera. The disadvantage of this approach is that for example a default DoKeyCommand must be written three times, for TApplication, TDocument and TView, where the code for TDocument.DoKeyCommand and TView.DoKeyCommand is identical.
> 
> Identical code is clumsy. Therefore my wish that a helper object could somehow be put in the namespace of the importing object, including the ability to override the imported methods. I hope that answers you question.

This makes perfect sense to me but see what Sven says. I've encountered this same problem many times over the years where you want code modularity but can't use inheritance. There is absolutely a need for this syntax in my opinion and that fact Apple is holding developer conferences over it further shows this (https://developer.apple.com/videos/wwdc/2015/?id=408). 

The interface delegation has so much boiler plate and doesn't help with the namespace issues so I'm not sure why it's better then making your TQDGraphPort and TEventHandler records and just including them as fields in the main class. I think they were designed for some other pattern that I'm not familiar with myself. The idea of traits/aspects is similar but falls under the umbrella of class composition as an alternative to inheritance.

The article I posted before (http://machinethink.net/blog/mixins-and-traits-in-swift-2.0/) actually has some good use case examples including pretty charts and pictures. Swift uses protocol extensions to accomplish "Protocol-Oriented Programming" which would be the equivalent of interface helpers in Object Pascal. We can't use fields in interfaces or helpers though so this won't work for us I'm afraid.

program mixin;

type
  IBrain = interface
    procedure Eat;
  end;

type
  IPhysics = interface
    procedure Apply;
  end;

type
  TPhysicsHelper = interface helper for IPhysics
    procedure Apply;
  end;

procedure TPhysicsHelper.Apply;
begin
  // ... we can't add fields in helpers or interfaces!
end;

type
  TPerson = class(IBrain, IPhysics)
  end;

begin
  // calls TPhysicsHelper.Apply via the IPhysics interface in TPerson
  person.Apply;
end.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list