Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:00:00+00:00 2026-05-16T06:00:00+00:00

In my application, I’ve created the TList type list, intended to store Integers or

  • 0

In my application, I’ve created the TList type list, intended to store Integers or Doubles:

TKList<T> = class
  private
    FItems: TList<T>;
    function GetItem(Index: Integer): T;
    procedure SetItem(Index: Integer; const Value: T);
    function GetMaxValue(): T;
    function GetMinValue(): T;
  public
    constructor Create; overload;
    constructor Create(const AKList: TKList<T>); overload;
    destructor Destroy; override;
    procedure Assign(const AKList: TKList<T>);
    function Add(const AValue: T): Integer;
    procedure Clear;
    function Count: Integer;
    procedure Invert;
    function ToString: string; override;
    function Info: string;
    property Values[Index: Integer]: T read GetItem write SetItem; default;
  end;

How can I implement Invert() procedure to invert values in generic List?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:00:00+00:00Added an answer on May 16, 2026 at 6:00 am

    There’s no way to specify constraints on generics such that you can require the types to be numbers, so there’s no way you can use numeric operators on the values in your list. Craig Stuntz wrote a series of posts describing how to build a generic statistical library, and he came up against the same problem. He solved it by providing additional arguments to his functions so that the caller could provide implementations for the type-specific numeric operations — the template method pattern. Here’s how he declared the Average operation:

    type
      TBinaryOp<T> = reference to function(ALeft, ARight: T): T
    
      TStatistics<T> = class
        public
          class function Average(const AData: TEnumerable<T>;
                                 AAdder, ADivider: TBinaryOp<T>;
                                 AMapper: TFunc<integer, T>): T; overload;
    

    Callers of that function need to provide their own code for adding, dividing, and “mapping” the generic type. (Mapping is covered in a later post and isn’t important here.) You could write your Invert function like this:

    type
      TUnaryOp<T> = reference to function(Arg: T): T;
    
      TKList<T> = class
        procedure Invert(ANegater: TUnaryOp<T>);
    
    procedure TKList<T>.Invert;
    var
      i: Integer;
    begin
      for i := 0 to Pred(Count) do
        Values[i] := ANegater(Values[i]);
    end;
    

    To make it more convenient to call the methods without having to provide the extra arguments all the time, Stuntz showed how to declare a type-specific descendant that provides the right arguments. You could do it like this:

    type
      TIntKList = class(TKList<Integer>)
      private
        class function Negate(Arg: Integer): Integer;
      public
        procedure Invert;
      end;
    
    procedure TIntKList.Invert;
    begin
      inherited Invert(Negate);
    end;
    

    You can provide type-specific descendants for the common numeric types, and if consumers of your class need to use other number-like types, they can provide their own implementations for the basic numeric operations without having to re-implement your entire list class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application settings in Windows Forms is a convenient way to store application-wide properties. However,
Application shows information about planets, their moons, and etc. It shows a list of
My application that is not on Play Store verify on the web If there
application.rb config.time_zone = 'Athens' I want to list entries which are published between any
/* * Application the reads an integer and prints sum of all even integers
Application : Nerddinner. This SP is for an inserting password for newly created User.
Application: WPF Application consisting of a textbox on top and a listbox below Users
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application is written in delphi 2010 and the underlying dll is a C++ dll.
Application[temp] = 8; should set the value 8 to the key temp . However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.