Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-06

Overloading properties

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Fuchs

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---

leledumbo
to Michael Fuchs
Of course I could rename these methods and add a prefix like Set..., but I

want to know why this limitation is

Can you create a variable and a function with the same name under the same scope? If you could answer that, you'll know why. As something you might want to think about: properties call is essentially a function call.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Overloading-properties-tp5715442p5715449.html Sent from the Free Pascal - General mailing list .