Is it possible to use two record helpers for the string type?
Asked Answered
S

4

11

I created this helper in order to add some more functions to the string type:

type
  AStringHelper = record helper for string
    function Invert: string; overload;
    function InvertMe: string; overload;
  end;

But when I use it in my code, the TStringHelper in System.StrUtils "gets out" and I can't use it's functions.

Is it possible to both of them to coexist?

Saltwort answered 27/7, 2013 at 15:56 Comment(2)
FWIW, Reverse is a more commonly used name for what I think your function does.Sagerman
I used to use Reverse too...Saltwort
S
13

At most one helper can active at any one point in your code. The documentation says this:

You can define and associate multiple helpers with a single type. However, only zero or one helper applies in any specific location in source code. The helper defined in the nearest scope will apply. Class or record helper scope is determined in the normal Delphi fashion (for example, right to left in the unit's uses clause).

Since record helpers do not support inheritance, there's no way to have both the standard helper's and your helper's functionality active at the same time, other than re-implementing the standard helper's functionality.

Sagerman answered 27/7, 2013 at 16:29 Comment(4)
Indeed, reimplementing would be easiest, simply by using the TStringHelper functions to implement the reimplementation, IOW using aggregation instead of inheritance.Stanton
@RudyVelthuis: What do you meant re-implementing? Can you give an example?Zales
Simply write a new string helper and use the existing string helper to implement it. Problem is that a helper can not have its own instance variables, though.Stanton
Five years later and it's still not possible to have custom StringHelper functions additionally to the existing ones (without copying/modifying the original unit or odd workarounds like the other answers here)? This is so frustrating. Any idea why Embarcadero still doesn't allow record helper inheritance?Die
B
7

Try to create your own class and helper

TMyString = type string;
TMyStringHelper = record helper for TMyString 
  function Invert: TMyString; 
  function ToString: string;
end;

Use TMyString instead of String.

To use standard String helpers wrap TMyString variable with String() or use ToString method.

var
  S1: TMyString;
  S2: String;
begin
  S1 := ' 123456 ';
  S1.Invert;
  S2 := String(S1).Trim;
  S2 := S1.ToString.Trim;
end;
Bemire answered 19/7, 2016 at 9:33 Comment(1)
I actually tried this out and it is working perfectly. The only thing you might have to do is put a string into your TMyString value before you can use your TMyString record hleperHein
S
4

Another possibility is to use the "Ancestor list" option offered by helpers

Syntaxis:

type
identifierName = class|record helper [(ancestor list)] for TypeIdentifierName
  memberList
end;

And use like this:

Unit1:   
THelperBase = Class helper for TMyClass
Public
  Function SayHello;
end;

Unit2:
Uses Unit1;
THelperChild = Class helper (THelperBase) for TMyClass
Public
  Function SayGoodBye;
end;

And finally, in your code:

Uses Unit2;   

Var MyClass: TMyclass;   

MyClass:= TMyclass.Create;
MyClass.SayHello; //valid
MyClass.SayGoodBye; //valid
Sharl answered 13/2, 2019 at 15:2 Comment(1)
"The ancestor list is optional. It can be specified only for class helper." So this seems to be a solution for class helpers, but not for record helpers :-(Quicksilver
M
3

A simple solution to this could be:

type
  TStr = record
  private
    FStr:string;
  public
    function Invert: string; overload;
    function InvertMe: string; overload;
  end;

By using type casting you can access the functions on TStr on a ordinary string variable:

var
  s1,s2:string;
begin
  s1:='123456';
  s2:=TStr(s1).invert;
end;

But of course, then we can ask: Why not just write an ordinary function? :-)

Anyway, I like this class / object / record method syntax better than traditional function/procedure.

Monique answered 15/12, 2015 at 13:18 Comment(1)
Because the dudes think that this object method ways are better and we should not use them mixed with directly function calls. :-/Saltwort

© 2022 - 2024 — McMap. All rights reserved.