[fpc-devel] [] property overloads

Ryan Joseph genericptr at gmail.com
Fri Jun 21 19:59:08 CEST 2019


Can anyone test this to see if it’s ok? Not sure I got everything correct and I still need some new error messages that make more sense (see the writeln’s I left in)

Even though overloads are allowed the actual name isn’t overloaded so you MUST access without the name to get overloading affects. I didn’t overload the actual name because properties aren’t really functions and as such don’t use tcallcandiates etc… If you attempt to use the name of the default property the last property declared wins. Is that ok? I hope so because I imagine it’s lots of work to actually make property names behave like real functions and not worth it imo.

https://github.com/genericptr/freepascal/tree/array_prop_overload

{$mode objfpc}
{$modeswitch advancedrecords}

program tarrpropol1;

// https://bugs.freepascal.org/view.php?id=29056

type
  TValue = TObject;

function GetGlobalValue(index: integer): TValue;
begin
  result:=nil;
end;

property Values[index: integer]: TValue read GetGlobalValue;
//property Values[index: string]: TValue read GetGlobalValue;

type
  TList = record
    function GetFirst: TValue;
    property First: TValue read GetFirst;

    function GetValueWithInt(index: integer): TValue;
    function GetValueWithString(index: string): TValue;
    function GetValueWithPair(index: integer; key: string): TValue;

    { all default properties must share the same name }
    //property Values0[index: integer]: TValue read GetValueWithInt; default;
    //property Values1[index: string]: TValue read GetValueWithString; default;

    { default properties must have unique parameters }
    //property Values[index: integer]: TValue read GetValueWithInt; default;
    //property Values[index: integer]: TValue read GetValueWithInt; default;

    { parametered properties still can't have duplicate names }
    //property Values[index: integer]: TValue read GetValueWithInt;
    //property Values[index: string]: TValue read GetValueWithString;

    { ok-defaults }
    property Values[index: integer]: TValue read GetValueWithInt; default;
    property Values[index: string]: TValue read GetValueWithString; default;
    property Values[index: integer; key: string]: TValue read GetValueWithPair; default;
  end;

function TList.GetFirst: TValue;
begin
  result:=nil;
end;

function TList.GetValueWithInt(index: integer): TValue;
begin
  writeln('GetValueWithInt');
  result := nil;  
end;

function TList.GetValueWithString(index: string): TValue;
begin
  writeln('GetValueWithString');
  result := nil;  
end;

function TList.GetValueWithPair(index: integer; key: string): TValue;
begin
  writeln('GetValueWithPair');
  result := nil;  
end;

var
  c: TList;
  v: TValue;
begin
  v := c[1,'f'];
end.


Regards,
	Ryan Joseph


Regards,
	Ryan Joseph



More information about the fpc-devel mailing list