As all my life I specialized on variables, arguments ... it is a little bit difficult to switch to specialize type.
I'm thinking and reading stuff on the topic, so may be I could switch my mind to type specialization.
Another thing is if I have to create classes for every generic and specialization class combination. For example
TMyClass = class
end;
TMyClassForList = TList<TMyClass>;
TMyClassForDoubleLinkedList = TList<TMyClass>;
TMyClassForSingle....
I want to introduce one new class in the project and actually I have to introduce 5, 10 depending on what I would like to use it for.
Despite the extra classes introduced, I really don't like to specify what the class will be used for where it is declared. (I declare TMyClass but now this unit will use also the units where it will be used)
So, to avoid this I have to move this type specs in separate unit.
Another thing is that, for me it looks like the parameterized type is more like configuring the TList class.
TList class is doing the job, everything is implemented, but I want to give some more information how I want it to work.
No new methods or fields I want to introduce, no method to override also, so why I need a descendant class.
Also if I have hierarchy, I have to repeat the procedure
TMyClass1 = class
end;
TMyClass1ForList = TList<TMyClass1>;
TMyClass1ForDoubleLinkedList = TList<TMyClass1>;
TMyClass1ForSingle....
TMyClass2 = class(TMyClass1)
end;
TMyClass2ForList = TList<TMyClass2>;
TMyClass2ForDoubleLinkedList = TList<TMyClass2>;
TMyClass2ForSingle....
Also I tried to specialize the type and it still not working (for variable assigning and argument passing)
unit Unit1;
{$mode delphi}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
Generics.Collections;
type
TListWithObject = TList<TObject>;
TMyClass = class
public
someText: String;
end;
TListWithMyClass = TList<TMyClass>;
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
public
procedure doit();
procedure doListWithObject(list: TListWithObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.doit();
var
myObjList: TListWithMyClass;
var2: TListWithObject;
begin
myObjList := TListWithMyClass.Create();
var2 := myObjList;
doListWithObject(myObjList);
end;
procedure TForm1.doListWithObject(list: TListWithObject);
begin
Label1.Caption := IntToStr(list.Count);
end;
end.
Have compilation error
unit1.pas(56,11) Error: Incompatible types: got "TList<Unit1.TMyClass>" expected "TList<System.TObject>"
Plz give us some more code, I am a certified Borland/Inprise/Embarcadero CppBuilder and Delphi trainer (certified upto XE2).
I can prepare some real code example, but the code above illustrates the issue.
And I need to know which generics framework you use: FreePascal has several, all good but all slightly different to work with.
Note that if you write such a framework yourself, you might encounter some, but not many, typecasts in the framework internals if the generics you write are pointer based,
things like hardcasting to T, which would be bad design.
I'm using Generics.Collections. But I thought that the typecast is not related to specific usage of generics. (may be I'm missing something)