Link to home
Start Free TrialLog in
Avatar of QwertyNickNacker
QwertyNickNacker

asked on

What is the Delphi equivalent of a C++ template?

I'm trying to create list classes based on the TList class (e.g. TIntegerList, TRealList, TMyRecordList, etc.), and I'm looking for an elegant way to do so without copying the source code over and over again with the necessary modifications.

I thought of using something like the templates of C++, but I'm not familiar with its Delphi counterpart.

To be more specific, I modified the TList class that instead of being a list of pointers, it will be a list of TItem - which is a type I defined as follows:

type
  TItem = Integer;

and thus created TItemList, which is a list of integers. Changing TItem's definition to TItem = Real for example, will create a list of real numerical values, etc. The problem is that this definition is too static, and if i need two different list classes, i would have to copy the source code twice, change the classes' names, and redifine each of the classes' TItem type. In C++, I can use an element called a Template to leave the type of TItem "open", and any class that inherites from TItemList can determines the type of TItem, thus creating a list of whatever type of elements i desire.

I'm looking for a way to do the same in Delphi. or perhaps a better suggestion to implement several list classes, each with a different type of list items...
Avatar of raidos
raidos

Not sure what C++ templates are, but inheriting from other classes is the way to do it if you ask me..


Type
  TMyTemplateClass = Class(TList)
    Protected
    Private
    Public
      Constructor Create;
      Destructor Destroy;
      Procedure MySpecialProc;
  End;
 
  TMyIntegerList = Class(TMyTemplateClass)
    Protected
    Private
    Public
  End;    

Implementation
 
Constructor TMyTemplateClass.Create;
Begin
  Inherited;
  //Special inits down below;
End;

Destructor TMyTemplateClass.Destroy;
Begin
  //Destroy stuff that needs to be destroyed
  Inherited;
End;

Procedure TMyTemplateClass.MySpecialProc;
Begin

End;

Hope you get the general idea.
Regards
//raidos
adding, getting, setting the item is also easy

you can check out my TlnDynBuffer or TSections at :
http://lee.nover.has.his.evilside.org/isapi/pas2html.dll/pas2html?File=/delphi/MiscFiles/vn_common/lnVidTypes.pas



C++ templates were an attempt to achieve through inheritance what modern OO languages do through interfaces.

In delphi you can inherit from a class and/or you can inherit an interface.  This is more like Java than C++.

type
    // for full equivalence to the C++ templating
    IMyStuff = interface (IUnknown)
        methods
        properties
    end;

    // Normally this is all you will need
    myList = class (Tlist)
        methods
        properties
    end;

    // Obviously you can have descendents of many classes that also inherit the interface
    myInterfacedList = class (TList, iMyStuff)
        methods
        properties
    end;

    myInterfacedControl = class (TControl, IMyStuff)
        methods
        properties
    end;

Anything declared in the interface must be implemented in the class.  
I think C++ templates have been created to give programmers the possibility to write datatype independent sourcecode. Templates are mainly used to implement algorithms, I do not know where the inheritance is hidden ;-) To my mind C++ is a very modern OO language, Delphi is copying C++ more and more.

regards
Omycron
Avatar of Tomas Helgi Johannsson
Hi!
Yes, Omycron is right, the purpose of C++ templates is to give the possibility to write datatype independent sourcecode.
To use C++ template A with class B the methods and operators of A must be declared in B to take full advantage of A.
And yes, it is similar to the use of Interfaces. But I have found out that interfaces are not as flexible as templates.

Regards,
  Tomas Helgi
Fair enough.  My understanding was that Java pioneered the "interface" (which was later mimicked by Delphi) because of problems inherent in the C++ template approach.  In retrospect, I believe the "problems" were more of design philosophy than technical nature.  This is in line with TomasHelgi's comment that interfaces are less flexible.  They certainly create a different design paradigm.

In that regard I would say that since there is no compiler string substitution facilities for the application code in Delphi/Pascal, there is no direct equivalent to the template.
ASKER CERTIFIED SOLUTION
Avatar of Calvin Day
Calvin Day
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QwertyNickNacker

ASKER

Hey,
 
  I didn't find the equivalent of C++ templates, but your solution the best one, and the one the one i eventually used, so keep on the good work dude,
                                 
                                QwertyNickNacker.

P.S.
  Variants rule.