[fpc-pascal] Overloading properties

Michael Fuchs freepascal at ypa-software.de
Sat Jun 22 17:41:03 CEST 2013


Hello,

I try to define classes with properties an additional fluent setters. 
Unfortunally the compiler does not allow the fluent methods have the 
same names as the properties.

project1.lpr(18,24)
Error: overloaded identifier "MyString" isn't a function

Of course I could rename these methods and add a prefix like Set..., but 
I want to know why this limitation is. And if it is possible to get rid 
of it and allow the overloading of properties with methods.


Thanks
Michael

And please take a look to the example program:


--->8------>8------>8------>8------>8------>8------>8---

program OverloadingTest;
{$mode objfpc}{$H+}

uses
   Classes, SysUtils;

type
   TMyObject = class(TObject)
     private
       FMyBoolean: Boolean;
       FMyInteger: Integer;
       FMyString: String;
     public
       property MyString: String read FMyString write FMyString;
       property MyBoolean: Boolean read FMyBoolean write FMyBoolean;
       property MyInteger: Integer read FMyInteger write FMyInteger;
     public
       function MyString(Value: String): TMyObject;
       function MyBoolean(Value: Boolean): TMyObject;
       function MyInteger(Value: Integer): TMyObject;
   end;

function TMyObject.MyString(Value: String): TMyObject;
begin
   FMyString := Value;
   Result := Self;
end;

function TMyObject.MyBoolean(Value: Boolean): TMyObject;
begin
   FMyBoolean := Value;
   Result := Self;
end;

function TMyObject.MyInteger(Value: Integer): TMyObject;
begin
   FMyInteger := Value;
   Result := Self;
end;


var
   o: TMyObject;

begin
   o := TMyObject.Create.MyString('Test').MyBoolean(True).MyInteger(42);
end.

--->8------>8------>8------>8------>8------>8------>8---



More information about the fpc-pascal mailing list