Recent

Author Topic: Lazarus, helper properties of freepascal  (Read 12429 times)

staratel20

  • Full Member
  • ***
  • Posts: 206
Lazarus, helper properties of freepascal
« on: March 26, 2016, 08:53:15 am »
Good day.

Often needed some List with additional properties. Easiest example - TListBox with boolean property Checked, and for example we need also boolean property Founded. So, very abstract(and only for example), can looks like:
Quote
TCheckedList=class
  property Item[AIndex:integer]:TStringList read GetItem;default;
  property Checked[AIndex:integer]:TBoolList read GetChecked;
  property Founded[AIndex:integer]:TBoolList read GetState;
end;

MyStr :TCheckedList;
after that we access to properties:

Code: Pascal  [Select][+][-]
  1.  
  2. MyStr.Checked[i];  
  3. MyStr.Founded[i];   // .. and so on. looks poor
  4.  
  5.  

What I propose. Syntax, something like:

Quote
TCheckedList=class
  property Item[AIndex:integer]:TStringList read GetItem;default;
  property Checked[AIndex:integer]:TBoolList read GetChecked;attached;  // attached to default property
  property Founded[AIndex:integer]:TBoolList read GetState;attached;  // attached to default property
end;

and after that we can write the same like:

Code: Pascal  [Select][+][-]
  1. MyStr[i].Checked;   // will call GetChecked[i]
  2. MyStr[i].Founded;   // will call GetFounded[i]
  3.  

.. and so on. looks good  :)

Interest what you think about it.

« Last Edit: March 28, 2016, 12:53:29 am 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

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: "Synchronous" property problem
« Reply #1 on: March 26, 2016, 09:05:14 am »
You can use a type helper for the Item type to achieve just that!.
I'll see if I can come up with a small example later.
Specialize a type, not a var.

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: "Synchronous" property problem
« Reply #2 on: March 26, 2016, 09:18:10 am »
I would thanks a lot)
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

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: "Synchronous" property problem
« Reply #3 on: March 27, 2016, 02:31:35 pm »
I read carefully this article(and continue discover it):
http://wiki.freepascal.org/Helper_types

but not sure that is possible realize what I'm talking about without changing to FPC core. At first if I'm extend TStringList with class TCheckedStringList - property Items are already busy. But I don't need changing behaviour of Items-property. So more deployed I need next behaviour:

Quote
s:string;
s:=MyStr;  // will call Items of class TStringList
MyStr.Checked;   // will call GetChecked of TCheckedStringList
MyStr.Founded;   // will call GetFounded of TCheckedStringList

Also I think that my suggested syntax is more simple and intuitive. Can I hope that something kind of this will be realized in next FPC versions?
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: 2770
    • havefunsoft.com
Re: "Synchronous" property problem
« Reply #4 on: March 27, 2016, 10:04:40 pm »
why TCheckedList returns lists ? (TStringList, TBoolList?)
Is it some sort of 2d-array... a list of lists?

But if you need to add these properties to TListBox, this is what you could do using helpers
Code: Pascal  [Select][+][-]
  1. type
  2.   TListBoxChecked = class helper for TListBox
  3.   protected
  4.     function GetChecked(i: integer): Boolean;
  5.   public
  6.     property Checked[i: integer]: Boolean read GetChecked;
  7.   end;
  8. ...
  9. function TListBoxChecked.GetChecked(i: integer): Boolean;
  10. begin
  11.   if not Assigned(Objects[i]) then Result:=false
  12.   else Result:=TMyObject(Objects[i]).checked;
  13. end;
  14.  
The actual implementation of .GetChecked() (or SetChecked()) depends on how you store "checked" value for an item within TListBox.
« Last Edit: March 27, 2016, 10:11:05 pm by skalogryz »

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: "Synchronous" property problem
« Reply #5 on: March 27, 2016, 10:57:54 pm »
>>why TCheckedList returns lists ?
it's I wrote only for example, on quick hand.  My code implementation is define the class, inherited from TStringList:

Code: Pascal  [Select][+][-]
  1. TMyClass=class(TStringList)
  2.   Checked:TBoolList;
  3.   Founded:TBoolList;  
  4. end;
  5.  

But writing something like:
Code: Pascal  [Select][+][-]
  1. m:TMyClass;
  2.  
  3. m[i]:='aaa';
  4. m.Checked[i]:=True; // looks bad for me
  5.  
  6. //  I would prefer to write:
  7. m[i].Checked:=True;
  8.  

is it possible to achieve?
« Last Edit: March 27, 2016, 11:14:24 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: 2770
    • havefunsoft.com
Re: "Synchronous" property problem
« Reply #6 on: March 27, 2016, 11:29:24 pm »
is it possible to achieve?
Yes. You'll have to use FPC 3.0.x. Won't work with FPC 2.6.4


staratel20

  • Full Member
  • ***
  • Posts: 206
Re: "Synchronous" property problem
« Reply #7 on: March 28, 2016, 12:06:23 am »
I use FPC 3.0, can you give me a pair lines of code how to do this?
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: 2770
    • havefunsoft.com
Re: "Synchronous" property problem
« Reply #8 on: March 28, 2016, 12:20:11 am »
I use FPC 3.0, can you give me a pair lines of code how to do this?
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch advancedrecords}
  3.  
  4. uses
  5.   StrUtils, Classes;
  6.  
  7. type
  8.   TBoolList = array of Boolean; // whatever. doesn't have to be the dynamic array
  9.  
  10. type
  11.   TMyClass = class;
  12.  
  13.   _MyClassInternal = record
  14.   private
  15.     function GetChecked: Boolean;
  16.     procedure SetChecked(AChecked: Boolean);
  17.   public
  18.     idx : integer;
  19.     cl  : TMYClass;
  20.     t   : string;
  21.     property Checked: Boolean read GetChecked write SetChecked;
  22.   end;
  23.  
  24.  
  25.   TMyClass = class(TStringList)
  26.   protected
  27.     function GetIntItem(aidx: Integer): _MyClassInternal;
  28.     procedure SetIntItem(aidx: integer; const m: _MyClassInternal);
  29.   public
  30.     Checked: TBoolList;
  31.     Founded: TBoolList;
  32.     property Items[i: integer]: _MyClassInternal read GetIntItem write SetIntItem; default;
  33.   end;
  34.  
  35.  
  36. operator := (s : string) m : _MyClassInternal;
  37. begin
  38.   m.t:=s;  
  39. end;
  40.  
  41. procedure TMyClass.SetIntItem(aidx: integer; const m: _MyClassInternal);
  42. begin
  43.   Strings[aidx]:=m.t;
  44. end;
  45.  
  46. function _MyClassInternal.GetChecked: Boolean;
  47. begin
  48.   Result:=cl.Checked[idx];
  49. end;
  50.  
  51. procedure _MyClassInternal.SetChecked(AChecked: Boolean);
  52. begin
  53.   cl.Checked[idx]:=AChecked;
  54. end;
  55.  
  56.  
  57. function TMyClass.GetIntItem(aidx: Integer): _MyClassInternal;
  58. begin
  59.   Result.idx:=aidx;
  60.   Result.cl:=Self;
  61. end;
  62.  
  63. var
  64.   m :  TMyClass;
  65. begin
  66.   m:=TMyClass.Create;
  67.   // just init
  68.   m.Add('');
  69.   SetLength(m.Checked, 1);
  70.   SetLength(m.Founded, 1);
  71.  
  72.   m[0]:='hello world';
  73.   writeln(m[0].checked);
  74.   m[0].checked:=true;
  75.   writeln(m[0].checked);
  76.   writeln(m.strings[0]);
  77. end.
  78.  

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: "Synchronous" property problem
« Reply #9 on: March 28, 2016, 12:24:58 am »
skalogryz: Very, very thank you.

Thaddy: I really try it by myself before you done, but unsuccessfully : ).

marcov: can you please rename the topic subject to "Simulation overloaded properties in Lazarus, helper classes" for better index it in google?
« Last Edit: March 28, 2016, 12:29:32 am 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: 2770
    • havefunsoft.com
Re: "Synchronous" property problem
« Reply #10 on: March 28, 2016, 12:33:53 am »
skalogryz: Very, very thank you.
if you ask my opinion, I cry tears of blood when I see code like this.

To mitigate my statement above, I can add that if a feature is available in the language, it doesn't mean it has to be used right away.
« Last Edit: March 28, 2016, 12:38:03 am by skalogryz »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: "Synchronous" property problem
« Reply #11 on: March 28, 2016, 12:36:37 am »
marcov: can you please rename the topic subject to "Simulation overloaded properties in Lazarus, helper classes" for better index it in google?
the subject: "Synchronous" property problem doesn't reflect the proposed solution. That's why marcov suggested to change the subject.
Thus any other person who would google for words "properties" "helpers" "freepascal" will run into this forum thread.

You can change the subject my editing your first post in this thread.

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: Lazarus, helper properties of freepascal
« Reply #12 on: March 28, 2016, 12:58:56 am »
Quote
To mitigate my statement above, I can add that if a feature is available in the language, it doesn't mean it has to be used right away.
not understand you clean. Only suspect that you say(in another words):

Quote
My code not looks good. With future FPC progress code may looks significant better. I not recommend you to use it right now.

Am I understand you correct?
« Last Edit: March 28, 2016, 01:01:09 am 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: 2770
    • havefunsoft.com
Re: Lazarus, helper properties of freepascal
« Reply #13 on: March 28, 2016, 01:05:19 am »
Quote
My code not looks good. With future FPC progress code may looks significant better. I not recommend you to use it right now.

Am I understand you correct?
No. Don't chase the sugar syntax at all. Don't chase java, javascript or C#.

It's pascal. It's closer to asm and C, where code efficiency is preferred over fancy looking dotted notation.

If you stop chasing the sugar syntax, it's very likely you'll no longer be dependent on the latest features of the compiler.

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: Lazarus, helper properties of freepascal
« Reply #14 on: March 28, 2016, 09:05:17 am »
Quote
No. Don't chase the sugar syntax at all. Don't chase java, javascript or C#.
I'm even don't thinking about it, I promise. And I don't  want Pascal to looks like Java or something else. I'm only want for to syntax of Pascal stay looks clean and human-friendly. And also follow the principle - don't produce redundant essence(but it wish closer to topic about overloaded properties).

Maybe you also hint me that I construct bad classes hierarchy(because if don't - so in fact you say - you task can be done only with code, that makes my eyes bleeding  :) ). And maybe - you right, To know it for sure, let me explain. In fact I try to build dynamic array of structures:
Code: Pascal  [Select][+][-]
  1. TMyItem=record
  2.   Data:string;
  3.   Checked:boolean;
  4.   Founded:boolean;
  5. end;
  6.  
  7. TListMyItem=class
  8.   property Items[AIndex:integer]:TMyItem read GetMyItem;
  9. end;
  10.  
  11. L:TListMyItem;
  12.  
  13. // and now correct:
  14. L.Items[0].Data:='str';
  15. L.Items[0].Checked:=True;
  16.  
  17. // but I wand have ability to write:
  18. L.Items[0]:='str';
  19. L.Items[0].Checked:=True;

This effect can be reached  if only records support 'default', but it's not support. If I'm write:

Quote
TMyItem=record
  Data:string;default;
  Checked:boolean;
  Founded:boolean;
end;

I've got error. Also if I go this way - I need to do all from scratch without using of already complete classes like TStringList and TBoolList.
« Last Edit: March 28, 2016, 09:07:19 am 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

 

TinyPortal © 2005-2018