Overloading properties

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Overloading properties

Michael Fuchs-5
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---
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading properties

leledumbo
Administrator
> 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.