Recent

Author Topic: My helper removes original helpers  (Read 1079 times)

Artur2024

  • Newbie
  • Posts: 4
My helper removes original helpers
« on: March 16, 2023, 11:14:17 pm »
This work
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch TypeHelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  10.  
  11. type
  12.   //HString = type helper for string
  13.   //  function MyClear(): String;
  14.   //end;
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     procedure FormClick(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. //function HString.MyClear(): String;
  34. //begin
  35. //  Result := '';
  36. //end;
  37.  
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormClick(Sender: TObject);
  42. var
  43.   s: string;
  44. begin
  45.   s := 'aaa';
  46.   ShowMessage(s.Replace('a', 'A'));
  47. end;
  48.  
  49. end.
  50.  

When I add my helpers, this code not work :(
"Replace" is: unit1.pas(46,17) Error: Illegal qualifier

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch TypeHelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  10.  
  11. type
  12.   HString = type helper for string
  13.     function MyClear(): String;
  14.   end;
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     procedure FormClick(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. function HString.MyClear(): String;
  34. begin
  35.   Result := '';
  36. end;
  37.  
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormClick(Sender: TObject);
  42. var
  43.   s: string;
  44. begin
  45.   s := 'aaa';
  46.   ShowMessage(s.Replace('a', 'A'));
  47. end;
  48.  
  49. end.
  50.  

How can I add my own helpers?

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: My helper removes original helpers
« Reply #1 on: March 16, 2023, 11:40:13 pm »
you can inherit from TStringHelper.

If memory serves, I think you specify TStringHelper instead of string.


I would need to look that up.

The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3238
  • POKE 54296,15
    • Eye-Candy Controls
Re: My helper removes original helpers
« Reply #2 on: March 16, 2023, 11:44:57 pm »
https://wiki.freepascal.org/Helper_types

Quote
By default, only one helper type can be active for a type at the same time. This helper is always the last one that is in scope (which follows the usual rules of Pascal).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: My helper removes original helpers
« Reply #3 on: March 16, 2023, 11:57:56 pm »
Code: Pascal  [Select][+][-]
  1.  TMyStringHelper = type helper(TStringHelper) for Ansistring
  2.    //
  3.   end;                                                          
  4.  
  5.  
The only true wisdom is knowing you know nothing

Artur2024

  • Newbie
  • Posts: 4
Re: My helper removes original helpers
« Reply #4 on: March 17, 2023, 12:23:24 am »
Thank you, now the code works  :D
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch TypeHelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  10.  
  11. type
  12.   TMyStringHelper = type helper(TStringHelper) for Ansistring
  13.     function MyClear(): String;
  14.   end;
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     procedure FormClick(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. function TMyStringHelper.MyClear(): String;
  34. begin
  35.   Result := '';
  36. end;
  37.  
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormClick(Sender: TObject);
  42. var
  43.   s: string;
  44. begin
  45.   s := 'aaa';
  46.   ShowMessage(s.Replace('a', 'A'));
  47.   s := s.MyClear();
  48. end;
  49.  
  50. end.
  51.  
  52.  

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: My helper removes original helpers
« Reply #5 on: March 17, 2023, 04:57:19 am »
https://wiki.freepascal.org/Helper_types

Quote
By default, only one helper type can be active for a type at the same time. This helper is always the last one that is in scope (which follows the usual rules of Pascal).

Yes. In next fpc version this limitation will be removed though. https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_multiple_active_helpers_per_type

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: My helper removes original helpers
« Reply #6 on: March 17, 2023, 07:55:14 am »
Correct, but beware: the concept suffers from the same pitfalls as multiple inheritance in C++ classes, it is the programmer's responsability to keep and maintain scope. So use with care.
Also note it needs a modeswitch, possibly for that reason.

The suggested inheritance is often the better option.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Artur2024

  • Newbie
  • Posts: 4
Re: My helper removes original helpers
« Reply #7 on: March 17, 2023, 10:00:45 am »
In my "MyClear" function, I lost the ability to use the "Copy" function. How can I deal with it?
Code: Pascal  [Select][+][-]
  1. function TMyStringHelper.MyClear(): String;
  2. begin
  3.   Result := Copy(Self, 1, 0);
  4. end;
unit1.pas(37,29) Error: Wrong number of parameters specified for call to "Copy"

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: My helper removes original helpers
« Reply #8 on: March 17, 2023, 10:29:32 am »
In my "MyClear" function, I lost the ability to use the "Copy" function. How can I deal with it?
Code: Pascal  [Select][+][-]
  1. function TMyStringHelper.MyClear(): String;
  2. begin
  3.   Result := Copy(Self, 1, 0);
  4. end;
unit1.pas(37,29) Error: Wrong number of parameters specified for call to "Copy"

The problem is with this method in TStringHelper, which hides System.Copy function.

Try
Code: Pascal  [Select][+][-]
  1. Result := System.Copy(Self, 1, 0);
« Last Edit: March 17, 2023, 10:36:49 am by Zoran »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: My helper removes original helpers
« Reply #9 on: March 17, 2023, 10:52:10 am »
Posts almost crossed, but indeed, specify system.copy.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Artur2024

  • Newbie
  • Posts: 4
Re: My helper removes original helpers
« Reply #10 on: March 19, 2023, 09:17:41 pm »
Everything works perfectly.
Thank you very much for your very quick help

 

TinyPortal © 2005-2018