[fpc-pascal] Traits Proposal

Ryan Joseph genericptr at gmail.com
Wed Feb 10 03:18:39 CET 2021


We had talked about this some time ago and it's been rattling around in my brain so I wanted to write it down into a formal proposal where we can discuss it and hopefully agree upon a syntax. Everything is preliminary and tentative but this is a syntax which allows a "composition over inheritance" model, also known as "mix-ins" in some languages. That idea is similar to multiple inheritance except you have a concrete reference to the trait being implemented so you can resolve conflicts easily.

Here's what I have so far. Please feel free to look at it and give any feedback.

https://github.com/genericptr/freepascal/wiki/Traits-Proposal

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

type
  TSomeTrait = trait
    public
      field: integer;
      procedure DoThis;
  end;
  
  TMyClass = class
    private
      _trait: TSomeTrait;
    public
      property someTrait: TSomeTrait implements _trait;
  end;

var
  c: TMyClass;
begin
  c := TMyClass.Create;
  c.DoThis;
  writeln(c.field):
end;

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list