Recent

Author Topic: Class helpers  (Read 6960 times)

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: Class helpers
« Reply #15 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 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Class helpers
« Reply #16 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: 648
Re: Class helpers
« Reply #17 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: 5444
  • Compiler Developer
Re: Class helpers
« Reply #18 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: 648
Re: Class helpers
« Reply #19 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