[fpc-pascal] Traits Proposal

Ryan Joseph genericptr at gmail.com
Thu Feb 11 03:07:55 CET 2021



> On Feb 10, 2021, at 2:42 PM, Ryan Joseph <genericptr at gmail.com> wrote:
> 
>  We should keep it as simple as possible imo.

It's not as elegant as other ideas we may think of but I think it's good enough to let users write boilerplate methods to resolve naming conflicts. We don't need to introduce new syntax or concepts. This is the power of traits vs multiple inheritance. We get the shared namespace like normal OOP inheritance but have full control over any conflicts that arise by mixing in traits.

====================

type
  TTraitA = trait
    procedure DoThis;
  end;
  TTraitB = trait
    procedure DoThis;
  end;
  
type
  TMyClass = class
    private
      property traitA: TTraitA implements _traitA;
      property traitB: TTraitB implements _traitB;
    public
      // DoThis now hides traitB.DoThis like it would in normal Object Pascal
      procedure DoThis;
  end;

procedure TMyClass.DoThis;
begin
  // resolve the conflict by directly referencing the trait
  traitA.DoThis;
end;

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list