Recent

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

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Lazarus, helper properties of freepascal
« Reply #15 on: March 28, 2016, 11:52:55 am »
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.

You can do that, if you overload assignment operator.

But you don't want to do that, because it breaks TMyItem interface consistency.
There're 3 attributes (Data, Checked, Founded) in the interface so far.
And the consistent way, is when you access each attribute by its name.
Code: Pascal  [Select][+][-]
  1. b[i].Data:='123123'
  2. b[i].Checked:=true;
  3. b[i].Founded:=true;
  4.  

What you're asking for is to break the consistency (not every attribute is accessed by its name) and even introduce duplication (where .Data could be access by either explicit name or by unnamed (default) operation)
Code: Pascal  [Select][+][-]
  1. b[i]:='123123';
  2. b[i].Data:='123123';
  3.  
The only benefit from that is you need to type 5 characters less.

It doesn't make the code more human-readable. Instead it might look puzzling, since exactly the same operation could be done in two different ways. And the code reader/maintainer has to keep in mind that .Data might be changed in a different way.

The less duplication - the better.

staratel20

  • Full Member
  • ***
  • Posts: 206
Re: Lazarus, helper properties of freepascal
« Reply #16 on: March 28, 2016, 05:03:22 pm »
Quote
It doesn't make the code more human-readable.
By this logic default property - makes code more puzzling too, because after definition you can write:

Code: Pascal  [Select][+][-]
  1. Items.Item[i]:='aaa';
  2. Items[i]:='aaa';
  3.  

and at this time this syntax not confused anyone. Want to thank you about the article about operators, I'll read it carefully.
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