* * *

Author Topic: Is in plans to realize overload properties?  (Read 4136 times)

staratel20

  • Full Member
  • ***
  • Posts: 199
Is in plans to realize overload properties?
« on: March 27, 2016, 07:40:45 pm »
Hi.

I think very often can be useful overload properties. For example:

MyClass=class
  property Items[AIndex:integer]:integer read ... write ...;overload;
  property Items[AKey:string]:integer  read ... write ...;overload;
end;

Also it would be useful for developers, that extends standard LCL-components. For example I download ECControls - very good set of components, but was some disappointment when property Checked of class TECCheckListBox need at least two parameters - Index and Column(because component support some checkbox per row). It is clean for me that the author have difficult choice - to create component with good compatibility of standard TCheckListBox - keep same property Checked and create new(like CheckedCol) or - modify Checked property, but lose compatibility. Overloaded properties can resolve this problems.

Thanks for your reading.
« Last Edit: April 20, 2016, 08:39:31 pm by staratel20 »
Windows 7 SP1 x64, FPC 3.0.0, Lazarus from trunk: http://svn.freepascal.org/svn/lazarus/trunk

CountIdentity, ModeClassName - good property naming
IdentityCount,  ClassNameMode  - bad property naming

wp

  • Hero Member
  • *****
  • Posts: 3450
Re: Is in plans to realize overload properties?
« Reply #1 on: March 27, 2016, 07:54:34 pm »
+1
Lazarus trunk / fpc 3.0.0 / Win32

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5338
Re: Is in plans to realize overload properties?
« Reply #2 on: March 27, 2016, 07:56:02 pm »
  property Items[AIndex:Variant]:integer read ... write ...;overload;

then in the getters and setters check aindex.vartype.

staratel20

  • Full Member
  • ***
  • Posts: 199
Re: Is in plans to realize overload properties?
« Reply #3 on: March 27, 2016, 08:23:33 pm »
marcov: No. I mean next:
Code: Pascal  [Select]
  1. MyClass=class
  2.   procedure GetItemsByIndex(AIndex:integer);
  3.   procedure SetItemsByIndex(AIndex:integer;AValue:integer);  
  4.  
  5.   procedure GetItemsByIndex(AKey:string);
  6.   procedure SetItemsByIndex(AIndex:integer;AKey:string);  
  7.  
  8.  
  9.   property Items[AIndex:integer]:integer read GetItemsByIndex write SetItemsByIndex;overload;
  10.   property Items[AKey:string]:integer  read GetItemsByStr write SetItemsByIndex;overload;
  11. end;
  12.  

As you see there no need to detect vartype, because the getters and setters have different [in] parameters.
« Last Edit: March 27, 2016, 08:48:36 pm by staratel20 »
Windows 7 SP1 x64, FPC 3.0.0, Lazarus from trunk: http://svn.freepascal.org/svn/lazarus/trunk

CountIdentity, ModeClassName - good property naming
IdentityCount,  ClassNameMode  - bad property naming

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5338
Re: Is in plans to realize overload properties?
« Reply #4 on: March 27, 2016, 08:25:40 pm »
I meant that you don't need overloading if you use variant

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1800
Re: Is in plans to realize overload properties?
« Reply #5 on: March 27, 2016, 08:28:10 pm »
staratel20, are you in need of overloading "default" property specifically?

so the following code works and compiles
Code: Pascal  [Select]
  1. var
  2.  m : myclass;
  3. ...
  4.  m[1]:=44;
  5.  m['hello']:=55
  6.  

staratel20

  • Full Member
  • ***
  • Posts: 199
Re: Is in plans to realize overload properties?
« Reply #6 on: March 27, 2016, 08:47:21 pm »
skalogryz: and yes and no. I need to code that you post to work(is this already can achieved, can you give link to full example?). And also this code should work(after reload properties implemented):

Code: Pascal  [Select]
  1. var
  2.  m : myclass;
  3. ...
  4.  m.Item[1]:=44;
  5.  m.Item['hello',True]:=55;
  6.  


marcov: I'm not clean understand how can variant type help me pass alternative count of parameters?

Try to speak more sharpen - for 'compiler eye' this are two different properties - Items[AIndex:integer] and Items[AKey:string] with own getters and setters methods, but for developer it's same named properties with different passed parameters
« Last Edit: March 27, 2016, 09:02:04 pm by staratel20 »
Windows 7 SP1 x64, FPC 3.0.0, Lazarus from trunk: http://svn.freepascal.org/svn/lazarus/trunk

CountIdentity, ModeClassName - good property naming
IdentityCount,  ClassNameMode  - bad property naming

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1800
Re: Is in plans to realize overload properties?
« Reply #7 on: March 27, 2016, 09:03:19 pm »
skalogryz: and yes and no. I need to code that you post to work(is this already can achieved, can you give link to full example?).
You can achieve something like this using class helpers.
Where a helper would re declare "Items" property adding a new way of accessing variables.
BUT in fpc 2.6.4 this would close the original "Items".

Not sure, if it has been changed in fpc 3.0.0 and/or fpc-trunk, but I know that there's a discussion to allow using of multiple class helpers.


the example:
Code: Pascal  [Select]
  1. type
  2.   TMyClass = class(TObject)
  3.   protected
  4.     function GetInt(const idx: integer): Integer;
  5.     procedure SetInt(const idx: integer; avalue: integer);
  6.   public
  7.     property Items[AIndex: integer]: integer read GetInt write SetInt; default;
  8.   end;
  9.  
  10.   THelpYourSelf = class helper for TMyClass
  11.   protected
  12.     function GetStr(const idx: string): Integer;
  13.     procedure SetStr(const idx: string; avalue: integer);
  14.   public
  15.     property Items[Aindex: string]: integer read GetStr write SetStr; default;
  16.   end;
  17.  

yet again, in FPC 2.6.4 it will work, but only for separate units.
Thus you will be able to do
  m [1]:=5;
in a unit that doesn't use the helper.
However, if a unit uses the unit with the helper declaration the only way to access properties would be
  m['hello']:=5;

It might be less strict in fpc 3.0.0 or fpc-trunk
« Last Edit: March 27, 2016, 09:06:34 pm by skalogryz »

staratel20

  • Full Member
  • ***
  • Posts: 199
Re: Is in plans to realize overload properties?
« Reply #8 on: March 27, 2016, 09:49:33 pm »
skalogryz: It is interesting using of helper classes, think I'll create a simple example and make a little addition to wiki. Maybe (if it will not difficult for you) you can also help me to resolve my problem, described in the neighbour topic:
http://forum.lazarus.freepascal.org/index.php/topic,32069.msg206402.html#msg206402
Thaddy was said that he create a little example, but maybe now to busy and at this time I hav't.


Want to notice, that I've not see the solution to resolve problem of compatible components, when his default property parameters are extended, as I describe in first post.

P/s: offtopic, but I'm don't understand why when I write Quick reply(or Reply) syntax highlighter highlight words 'neighbour' and 'behaviour'? Every time I doubt that I write it correct and verify in google translator - it's a bit bother.
Windows 7 SP1 x64, FPC 3.0.0, Lazarus from trunk: http://svn.freepascal.org/svn/lazarus/trunk

CountIdentity, ModeClassName - good property naming
IdentityCount,  ClassNameMode  - bad property naming

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1800
Re: Is in plans to realize overload properties?
« Reply #9 on: March 27, 2016, 10:00:34 pm »
P/s: offtopic, but I'm don't understand why when I write Quick reply(or Reply) syntax highlighter highlight words 'neighbour' and 'behaviour'? Every time I doubt that I write it correct and verify in google translator - it's a bit bother.
neighbour vs neighbor
behaviour vs behavior

btw, google translate also automatically changes neighbour to neighbor.

Don't be surprised, you were taught British version of English :)

staratel20

  • Full Member
  • ***
  • Posts: 199
Re: Is in plans to realize overload properties?
« Reply #10 on: March 27, 2016, 10:11:24 pm »
skalogryz: thanks for explaining this difference(also "my google" don't make auto changing - don't know why). But it's all about offtopic, and how about the topic : )?
Windows 7 SP1 x64, FPC 3.0.0, Lazarus from trunk: http://svn.freepascal.org/svn/lazarus/trunk

CountIdentity, ModeClassName - good property naming
IdentityCount,  ClassNameMode  - bad property naming

SnoopyDog

  • New member
  • *
  • Posts: 15
Re: Is in plans to realize overload properties?
« Reply #11 on: October 11, 2016, 11:36:56 am »
The question the first post is wrong: It should not be: "Plans to realize overload properties", it should be: "Can we get this feature back?" ;)

In FPC 2.6.4 which came up with Lazarus 2.4, overloaded properties were already supported. A code like
Code: [Select]
type cTestClass = class
  function GetValue (Idx : integer) : string; virtual; abstract; overload;
  function GetValue (Key : string) : string; virtual; abstract; overload;
  property Value [Idx : integer] : string read GetValue;
  property Value [Key : string] : string read GetValue;
end;

was already compiling and working. The only difference between Delphi and FPC/Lazarus was: In Delphi (i used 2007), this works ONLY for default properties. In FPC 2.6.4, only one of those properties could be declared as default (but it also works for non-default).

FPC 3.0 now throws an error that i duplicated "Value"
« Last Edit: October 11, 2016, 11:40:59 am by SnoopyDog »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5338
Re: Is in plans to realize overload properties?
« Reply #12 on: October 11, 2016, 12:59:56 pm »
ady compiling and working. The only difference between Delphi and FPC/Lazarus was: In Delphi (i used 2007), this works ONLY for default properties. In FPC 2.6.4, only one of those properties could be declared as default (but it also works for non-default).

I don't even get at the properties declarations with Delphi XE4. It stumbles on combinations of virtual and overload.

SnoopyDog

  • New member
  • *
  • Posts: 15
Re: Is in plans to realize overload properties?
« Reply #13 on: October 11, 2016, 01:10:58 pm »
Ok, in Delphi 10.1 (this is the only newer version that i have), you have to reorder the keywords like this:

Code: [Select]
type cTestClass = class
  function GetValue (Idx : integer) : string; overload; virtual; abstract;
  function GetValue (Key : string) : string; overload; virtual; abstract;
  property Value [Idx : integer] : string read GetValue; {$IFNDEF FPC }default;{$ENDIF}
  property Value [Key : string] : string read GetValue; {$IFNDEF FPC }default;{$ENDIF}
end;

And this now compiles and works in Delphi 2007, Rad Studio 10.1 and Lazarus 2.4 with FPC 2.6.4. But not in Lazarus 2.6 with FPC 3.0 anymore...

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5338
Re: Is in plans to realize overload properties?
« Reply #14 on: October 11, 2016, 01:44:50 pm »
Ok, in Delphi 10.1 (this is the only newer version that i have), you have to reorder the keywords like this:

I tried that before, but now I retested it and it works. Maybe the first time I only switched one, and was confused that the error was still on the first line.

Anyway, please file a bug, but it is nearly certain too late for 3.0.2 and maybe even the whole 3.0 branch. Languages features are rarely merged back to release versions.
« Last Edit: October 11, 2016, 01:51:44 pm by marcov »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus