Search code examples
delphiclass-helpers

How to call the original class's code when a class helper is in scope?


I'm doing some unit testing on an improved version of quicksort.
The (hopefully) faster version is implemented using:

TArrayHelper = class helper for System.Generics.Collections.TArray
....
   class procedure Sort<T>(var Values: array of T); overload; static;
   class procedure Sort<T>(var Values: array of T; Comparer: IComparer<T>); overload; static;
....

I know for now I can just rename the Sort into SortNew for the purpose of testing, but at some point I have to fix the method names to sort.

If there is a class helper in scope, how do I call the original methods?

Is there a way using rtti or some other roundabout method?


Solution

  • I think that the only way to achieve this with pure Pascal code is to call Sort from a scope in which your class helper is not active. The point being that if your class helper is active, then Sort refers to the method in the helper.

    For instance like this:

    unit ScopeBuster;
    
    interface;
    
    uses
      System.Generics.Collections;
    
    type
      TArrayScopeBuster = class
        class procedure Sort<T>(...); overload; inline; static;
      end;
    
    implementation
    
    class procedure TArrayScopeBuster.Sort<T>(...); 
    begin
      TArray.Sort(...);
    end;
    
    end.