Recent

Author Topic: How extend existing class like TStringHelper?  (Read 764 times)

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
How extend existing class like TStringHelper?
« on: November 27, 2022, 06:45:06 am »
I could not find any docs on writing code like TStringHelper, i.e. you can call the helper functions as if they are native to the string class. Need some examples.

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: How extend existing class like TStringHelper?
« Reply #1 on: November 27, 2022, 07:26:45 am »
Easy.
Code: Pascal  [Select][+][-]
  1. {$H+}{$mode objfpc}{$modeswitch typehelpers}
  2. uses sysutils;
  3. type
  4.   ThwHelper = type helper for AnsiString
  5.     function Say:ansistring;
  6.   end;
  7.  
  8.   function ThwHelper.Say:ansistring;
  9.   begin
  10.     result := 'Hello, World';
  11.   end;
  12.  
  13. var
  14.   a:ansistring;
  15. begin
  16.   writeln( a.say);
  17. end.

And https://wiki.freepascal.org/Helper_types#Type_Helper resp. https://www.freepascal.org/docs-html/ref/refch10.html

Reading manuals makes things understandable...

Specialize a type, not a var.

paweld

  • Hero Member
  • *****
  • Posts: 966
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: How extend existing class like TStringHelper?
« Reply #3 on: November 27, 2022, 07:29:28 am »
https://wiki.freepascal.org/Helper_types#Type_Helper
Yes, but you should also refer to the official documentation. The wiki is not always the best resource, and not official, although it comes up first in search results. "thanks" but no thanks to Google. Always read the manual.
« Last Edit: November 27, 2022, 07:35:02 am by Thaddy »
Specialize a type, not a var.

ssawgift

  • New Member
  • *
  • Posts: 48
    • My Personal Website
Re: How extend existing class like TStringHelper?
« Reply #4 on: November 27, 2022, 07:55:08 am »
Not sure what I have done wrong. I added the following unit to my project and it seems to break existing TStringHelper. I get many errors on existing code that uses TStringHelper functions.

Code: Pascal  [Select][+][-]
  1. unit Helpers;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch typehelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.  
  11. { Declaration }
  12. type
  13.   // String helpers
  14.   MyStringHelper = type helper for AnsiString
  15.     function IsArg: boolean;
  16.   end;
  17.  
  18. implementation
  19.  
  20. function MyStringHelper.IsArg: boolean;
  21. begin
  22.   Result := (Length(self) >= 2) and ((self[1] = '/') or (self[1] = '-'));
  23. end;
  24.  
  25. end.
  26.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: How extend existing class like TStringHelper?
« Reply #5 on: November 27, 2022, 08:13:20 am »
Use inheritance:
Code: Pascal  [Select][+][-]
  1. unit Helpers;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$modeswitch typehelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils;
  10.  
  11. { Declaration }
  12. type
  13.   // String helpers
  14.   MyStringHelper = type helper(TstringHelper) for AnsiString // extends, not replace.
  15.     function IsArg: boolean;
  16.   end;
  17.  
  18. implementation
  19.  
  20. function MyStringHelper.IsArg: boolean;
  21. begin
  22.   Result := (Length(self) >= 2) and ((self[1] = '/') or (self[1] = '-'));
  23. end;
  24.  
  25. end.
In trunk there is also a modeswitch that allows multi-helpers instead of inheritance.
https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_multiple_active_helpers_per_type
 
« Last Edit: November 27, 2022, 08:24:02 am by Thaddy »
Specialize a type, not a var.

paweld

  • Hero Member
  • *****
  • Posts: 966
Re: How extend existing class like TStringHelper?
« Reply #6 on: November 27, 2022, 08:39:27 am »
@Thaddy: In this case, the wiki article is ok, because I used to learn from it myself.
As for finding solutions I look first on the Lazarus wiki and then on the Lazarus forum. Only when I don't find a remote solution then only in the search engine "lazarus some_query" or optionally "delphi some_query" - I use DuckDuckGo, not Google :-)
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: How extend existing class like TStringHelper?
« Reply #7 on: November 27, 2022, 11:43:12 am »
I also use duckduckgo, but the results are the same: wiki entries are better indexed than the manuals.
Which is in my opinion plainly wrong.
« Last Edit: November 27, 2022, 11:47:39 am by Thaddy »
Specialize a type, not a var.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: How extend existing class like TStringHelper?
« Reply #8 on: November 27, 2022, 01:00:09 pm »
But at least now that the PDF links on the documentation page work properly it's comparatively easy to download them and search locally.

I don't know whether that fix has any implications for Google's willingness to index the PDFs, or whether there is any software which can be run locally that indexes better than Google... which has become progressively less useful for carefully-crafted text searching now that it no longer has competitors like AltaVista to worry about.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: How extend existing class like TStringHelper?
« Reply #9 on: November 27, 2022, 10:14:16 pm »
Not sure what I have done wrong. I added the following unit to my project and it seems to break existing TStringHelper. I get many errors on existing code that uses TStringHelper functions.

By default only one helper type can be active for a given type at a given time and it depends on what helper type is the last in scope (using usual scoping rules). If you want to keep an existing one active you need to use inheritance like Thaddy said. FPC main added the modeswitch MultiHelpers which allows multiple helpers to be active at once.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: How extend existing class like TStringHelper?
« Reply #10 on: November 27, 2022, 11:02:40 pm »
By default only one helper type can be active for a given type at a given time and it depends on what helper type is the last in scope (using usual scoping rules).

The error messages are fairly... obscure until you fully understand that point. After that they're crystal clear.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018