Recent

Author Topic: Class helpers  (Read 5578 times)

jamie

  • Hero Member
  • *****
  • Posts: 5129
Re: Class helpers
« Reply #15 on: May 13, 2019, 11:01:18 pm »
Yeah well I am glad you did that because I put it to heavy use! :)
The only true wisdom is knowing you know nothing

BeniBela

  • Hero Member
  • *****
  • Posts: 824
    • homepage
Re: Class helpers
« Reply #16 on: May 14, 2019, 12:46:38 am »
I do not understand why there are so many different helpers

Why not call them all "type helper"  ?

jamie

  • Hero Member
  • *****
  • Posts: 5129
Re: Class helpers
« Reply #17 on: May 14, 2019, 12:54:14 am »
I suppose the compiler could auto detect this via the object that is supplied so one don't need to specify which of the
types it wants to help.

 But what I was getting more to was the inherited feature to add on existing helpers.

The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 533
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: Class helpers
« Reply #18 on: May 14, 2019, 02:04:13 am »
Why not call them all "type helper"  ?

Hmm…

Code: Pascal  [Select][+][-]
  1. type
  2.   TFoo = class(TBar) // or any other type
  3.   {..}
  4.   end;
  5.  
  6. type
  7.   // instead of: TFooHelper = class helper for TFoo
  8.   TFooHelper = helper(TFoo)
  9.   {..}
  10.   end;
  11.  
  12. type
  13.   // instead of: TFooHelper2 = class helper(TFooHelper) for TFoo
  14.   TFooHelper2 = helper(TFooHelper)
  15.   // or
  16.   TFooHelper2 = helper(TFoo, TFooHelper)
  17.   {..}
  18.   end;

This should be enough. 8)
« Last Edit: May 14, 2019, 02:09:39 am by furious programming »
Lazarus 2.2.0 with FPC 3.2.2 (2022-01-02), Windows 10 — all 64-bit

PascalDragon

  • Hero Member
  • *****
  • Posts: 3634
  • Compiler Developer
Re: Class helpers
« Reply #19 on: May 14, 2019, 09:07:41 am »
I do not understand why there are so many different helpers

Why not call them all "type helper"  ?
Because it was Delphi that introduced class helper and record helper. True, it was me that introduced type helper for primitive types, but with FPC 3.2.0 and newer it's possible to use type helper with classes and records as well, so for me type helper is the definite syntax for helper types (though the other two will be available no matter what).
Code: Pascal  [Select][+][-]
  1. type
  2.   TFoo = class(TBar) // or any other type
  3.   {..}
  4.   end;
  5.  
  6. type
  7.   // instead of: TFooHelper = class helper for TFoo
  8.   TFooHelper = helper(TFoo)
  9.   {..}
  10.   end;
  11.  
  12. type
  13.   // instead of: TFooHelper2 = class helper(TFooHelper) for TFoo
  14.   TFooHelper2 = helper(TFooHelper)
  15.   // or
  16.   TFooHelper2 = helper(TFoo, TFooHelper)
  17.   {..}
  18.   end;

This should be enough. 8)
True, that would be enough, but we'd need to support the other syntax anyway due to Delphi compatibility. The type helper syntax on the other hand is a close enough extension.

devEric69

  • Hero Member
  • *****
  • Posts: 646
Re: Class helpers
« Reply #20 on: October 27, 2021, 09:32:48 am »
(old post, but related)

I wanted to create a helper for TFoo (I know that it's not recommended), like this...:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3. class helper for TFoo
  4.    private
  5.       FiCount: integer;
  6.    public
  7.       function GetThing: TObject;
  8.   end;

... but, as soon as I've added the private field FiCount in my " helper for " the Class Tfoo, then, the compiler "cried", saying: "uHelperForFoo.pas(40,8) Error: An interface, helper or Objective-C protocol or category cannot contain fields".

So, is there a\another hacking \ bandage for TClasses in order to bypass this behavior (i.e. create private fields), or do I have to create a descendant i.e. Class(TFoo) (that said, I've got the source code of TFoo; so, it's mostly curiosity)?
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3634
  • Compiler Developer
Re: Class helpers
« Reply #21 on: October 27, 2021, 09:37:32 am »
So, is there a\another hacking \ bandage for TClasses in order to bypass this behavior (i.e. create private fields), or do I have to create a descendant i.e. Class(TFoo) (that said, I've got the source code of TFoo; so, it's mostly curiosity)?

You need to create a proper descendant and use that. There is no other way.

devEric69

  • Hero Member
  • *****
  • Posts: 646
Re: Class helpers
« Reply #22 on: October 27, 2021, 10:09:46 am »
Thank you.
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018