Lazarus

Free Pascal => FPC development => Topic started by: staratel20 on March 27, 2016, 07:40:45 pm

Title: Is in plans to realize overload properties?
Post by: staratel20 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.
Title: Re: Is in plans to realize overload properties?
Post by: wp on March 27, 2016, 07:54:34 pm
+1
Title: Re: Is in plans to realize overload properties?
Post by: marcov 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.
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 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.
Title: Re: Is in plans to realize overload properties?
Post by: marcov on March 27, 2016, 08:25:40 pm
I meant that you don't need overloading if you use variant
Title: Re: Is in plans to realize overload properties?
Post by: skalogryz 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.  
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 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
Title: Re: Is in plans to realize overload properties?
Post by: skalogryz 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
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 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.
Title: Re: Is in plans to realize overload properties?
Post by: skalogryz 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 (http://grammarist.com/spelling/neighbor-neighbour/)
behaviour vs behavior (http://grammarist.com/spelling/behavior-behaviour/)

btw, google translate also automatically changes neighbour to neighbor.

Don't be surprised, you were taught British version of English :)
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 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 : )?
Title: Re: Is in plans to realize overload properties?
Post by: SnoopyDog 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"
Title: Re: Is in plans to realize overload properties?
Post by: marcov 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.
Title: Re: Is in plans to realize overload properties?
Post by: SnoopyDog 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...
Title: Re: Is in plans to realize overload properties?
Post by: marcov 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.
Title: Re: Is in plans to realize overload properties?
Post by: Ondrej Pokorny on October 12, 2016, 10:58:13 am
FPC supports overloaded properties - but it's a bug in FPC.

Just overload the getters/setters and don't redefine the property. See http://mantis.freepascal.org/view.php?id=28949 (http://mantis.freepascal.org/view.php?id=28949)
Title: Re: Is in plans to realize overload properties?
Post by: SnoopyDog on October 13, 2016, 01:20:18 pm
@ondrejpokorny: Thank you very much for sharing this information. You made my day :) Now i can use Lazarus 1.6 with my existing code! Just need to play with compiler switches...
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 on November 01, 2016, 11:00:41 am
FPC supports overloaded properties - but it's a bug in FPC.
if you call it 'bug' , not 'future', so is in plan to fix it? If so, I don't understand SnoopyDog's joy, because code will not work in future.
Title: Re: Is in plans to realize overload properties?
Post by: Thaddy on November 01, 2016, 11:23:28 am
feature may well be implemented in the future....I guess
Title: Re: Is in plans to realize overload properties?
Post by: staratel20 on November 01, 2016, 04:55:43 pm
Also want to add, that variant types less preferable than overloaded getters/setters. Because in 2nd case you understand all parameters types class works with by just looking at class declaration. In 1st case you need also jump/scroll to implementation + analyse code. Thus useless seconds accumulate to useless hours of "programming".
Title: Re: Is in plans to realize overload properties?
Post by: Blestan on March 03, 2017, 03:46:31 pm
in the end of the day i do not understands if this "feature" (allowing properies to use differnt type of indices ) is future safe or is just a bug in the compiler...
help apreciated because my code relies on this
thanks!
Title: Re: Is in plans to realize overload properties?
Post by: Thaddy on March 03, 2017, 04:37:18 pm
in the end of the day i do not understands if this "feature" (allowing properies to use differnt type of indices ) is future safe or is just a bug in the compiler...
help apreciated because my code relies on this
thanks!

What you need is a dictionary (or a map, same thing) . TDictionary<TKey.TValue> or TFPGMap<TKey,TValue>
Both of these can query for items of either type Key or Type Value. property Keys[], property Values[], much more clear.

Although the example code with the class helper still compiles in trunk, you will run into trouble implementing a sane result from it ;) But it still does work. And please use generics.


Title: Re: Is in plans to realize overload properties?
Post by: Awkward on March 03, 2017, 05:01:04 pm
if only i can realize my class for ini-files with default property where can use different count of array parameters... one, two or three...
Title: Re: Is in plans to realize overload properties?
Post by: Blestan on March 03, 2017, 05:29:20 pm
@Thaddy:  my own object notation (XON) is itself a dictioanry or if you call it a map- its a an super data structure capable of holding scalar values or arrays ( integer index) or objects ( string indices) ... for the elegance of the code it's better to be able to write:
 xvarA[1]:=1; // xvar.GetChildByIndex(1).AsInteger=1;
 xvarB['name']:='blaBlaBla'; xvar.GetChildByKey.AsString='BlaBlaBla';

i hole this will still work in the future
TinyPortal © 2005-2018