[fpc-pascal] overloaded property index

Luca Olivetti luca at ventoso.org
Fri Aug 12 16:34:46 CEST 2016


I'm still using fpc 2.6.4 (but I tested what I'm saying below with fpc 
3.0.0 too).

I need to overload the type of the index of an indexed property, e.g.

function GetMyProp(index:integer):TMyPropType;
function GetMyProp(index:TMyType):TMyPropType;
....
property MyProp[index:integer]:TMyPropType read GetMyProp;
property MyProp[index:TMyType]:TMyPropType read GetMyProp;


this gives a "Duplicate identifier" error.

I found previous discussions about this error and 
http://bugs.freepascal.org/view.php?id=29056

*but* if I omit the second property declaration, the code compiles fine 
and I can use MyProp indexed either by an integer or by TMyType.

Is this by design or is it just an accident?

I'm attaching a simple test program and apparently it does the "right 
thing".

Bye
-- 
Luca
-------------- next part --------------
program project1;
{$mode objfpc}{$H+}

uses
  Classes, SysUtils;

type

  TMyType = (mta, mtb, mtc);

  { TMyClass }

  TMyClass = class
  private
    function GetMyProp(index: integer): string;
    function GetMyProp(index: TMyType): string;
    { private declarations }
  public
    { public declarations }
    property MyProp[index:integer]:string read GetMyProp;
  end;

{ TMyClass }


function TMyClass.GetMyProp(index: integer): string;
begin
  result:='integer '+IntToStr(index);
end;

function TMyClass.GetMyProp(index: TMyType): string;
begin
  WriteStr(result,index);
  result:='MyType '+result;
end;

var i:integer;
    m:TMyType;

begin
  with TMyClass.Create do
  begin
    for i:=1 to 10 do
      writeln(MyProp[i]);
    for m in TMyType do
      writeln(MyProp[m]);
  end;
end.



More information about the fpc-pascal mailing list