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

Ryan Joseph genericptr at gmail.com
Thu Dec 26 20:32:44 CET 2019



> On Dec 24, 2019, at 1:16 AM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> Basically, yes, though of course syntax, implementation and behavior need to be nicely defined first. For example there is the difference whether something is added at declaration time of the class or the creation time of the class (though that might be the difference between mixins and aspect oriented programming). 
> 

I hope you're all having a wonderful Christmas season. :)

Some more thoughts on this:

- My initial thought was that importing would happen at definition time simply because I don't know what the difference would be to import at declaration time.

- Importing fields/properties is easy because we can just add symbols to the symbol table but I'm not sure what it means in terms of the compiler to import a method from another class. The method needs to be cloned or perhaps synthesized as I've seen the term used in the compiler.  Because the traits aren't actually allocated like a class or record we need to copy the code out into the class that uses them.

program mixin;

type
  TBrain = trait
    procedure DoStuff; virtual;
  end;

procedure TBrain.DoStuff;
begin
  writeln('do something');
end;

type
  TBase = class
    use TBrain;
  end;

// TBase.DoStuff is synthesized from TBrain.DoStuff
procedure TBase.DoStuff;
begin
  writeln('do something');
end;

begin
end.


- What about generics? Looks complicated and dubious so maybe that could be a feature added later simply in the interest of taking things in small steps.

program mixin;

type
  generic TBrain<T> = trait
    procedure DoStuff(param: T); virtual;
  end;

type
  generic TBase<T> = class
    private
      use specialize TBrain<T>;
    public
      procedure DoStuff(param: T); override;
  end;

begin
end.

- Is overriding allowed? This presents a dilemma because we need to synthesize the method from the trait (or whatever they will be called) and allow an additional method which can call "inherited" and get the super method in the same class. This would be new functionally for the inherited keyword since the overriding is happening in the same class.

program mixin;

type
  TBrain = trait
    procedure DoStuff; virtual;
  end;

procedure TBrain.DoStuff;
begin
  writeln('do something');
end;

type
  TBase = class
    use TBrain;
    procedure DoStuff; override;
  end;

// TBase.TBrain_DoStuff is synthesized from TBrain.DoStuff
// and renamed to prevent name collisions
procedure TBase.TBrain_DoStuff;
begin
  writeln('do something');
end;

procedure TBase.DoStuff;
begin
  // inherited calls TBase.TBrain_DoStuff
  inherited;
  writeln('do more stuff');
end;

begin
end.




Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list