Is in plans to realize overload properties? lazarus / 64-FPC development ---------------------------------------------------------------------- staratel20 · 2016-03-27 17:40 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. ---------------------------------------------------------------------- wp · 2016-03-27 17:54 +1 ---------------------------------------------------------------------- marcov · 2016-03-27 17:56 property Items[AIndex:Variant]:integer read ... write ...;overload; then in the getters and setters check aindex.vartype. ---------------------------------------------------------------------- staratel20 · 2016-03-27 18:23 marcov: No. I mean next: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyClass=class  procedure GetItemsByIndex(AIndex:integer);  procedure SetItemsByIndex(AIndex:integer;AValue:integer);     procedure GetItemsByIndex(AKey:string);  procedure SetItemsByIndex(AIndex:integer;AKey:string);      property Items[AIndex:integer]:integer read GetItemsByIndex write SetItemsByIndex;overload;  property Items[AKey:string]:integer  read GetItemsByStr write SetItemsByIndex;overload;end;  As you see there no need to detect vartype, because the getters and setters have different [in] parameters. ---------------------------------------------------------------------- marcov · 2016-03-27 18:25 I meant that you don't need overloading if you use variant ---------------------------------------------------------------------- skalogryz · 2016-03-27 18:28 staratel20, are you in need of overloading "default" property specifically? so the following code works and compiles Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var m : myclass;... m[1]:=44; m['hello']:=55 ---------------------------------------------------------------------- staratel20 · 2016-03-27 18:47 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var m : myclass;... m.Item[1]:=44; m.Item['hello',True]:=55;  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 ---------------------------------------------------------------------- skalogryz · 2016-03-27 19:03 Quote from: staratel20 on March 27, 2016, 08:47:21 pmskalogryz: 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyClass = class(TObject)  protected    function GetInt(const idx: integer): Integer;    procedure SetInt(const idx: integer; avalue: integer);  public    property Items[AIndex: integer]: integer read GetInt write SetInt; default;  end;   THelpYourSelf = class helper for TMyClass  protected    function GetStr(const idx: string): Integer;  procedure SetStr(const idx: string; avalue: integer);  public    property Items[Aindex: string]: integer read GetStr write SetStr; default;  end;  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 ---------------------------------------------------------------------- staratel20 · 2016-03-27 19:49 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.ht ml#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. ---------------------------------------------------------------------- skalogryz · 2016-03-27 20:00 Quote from: staratel20 on March 27, 2016, 09:49:33 pmP/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 · 2016-03-27 20:11 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 : )? ---------------------------------------------------------------------- SnoopyDog · 2016-10-11 09:36 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" ---------------------------------------------------------------------- marcov · 2016-10-11 10:59 Quote from: SnoopyDog on October 11, 2016, 11:36:56 amady 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 · 2016-10-11 11:10 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 · 2016-10-11 11:44 Quote from: SnoopyDog on October 11, 2016, 01:10:58 pmOk, 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. ---------------------------------------------------------------------- Ondrej Pokorny · 2016-10-12 08:58 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 ---------------------------------------------------------------------- SnoopyDog · 2016-10-13 11:20 @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... ---------------------------------------------------------------------- staratel20 · 2016-11-01 10:00 Quote from: ondrejpokorny on October 12, 2016, 10:58:13 amFPC 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. ---------------------------------------------------------------------- Thaddy · 2016-11-01 10:23 feature may well be implemented in the future....I guess ---------------------------------------------------------------------- staratel20 · 2016-11-01 15:55 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". ---------------------------------------------------------------------- Blestan · 2017-03-03 14:46 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! ---------------------------------------------------------------------- Thaddy · 2017-03-03 15:37 Quote from: Blestan on March 03, 2017, 03:46:31 pmin 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 or TFPGMap 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. ---------------------------------------------------------------------- Awkward · 2017-03-03 16:01 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... ---------------------------------------------------------------------- Blestan · 2017-03-03 16:29 @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