Recent

Author Topic: Need Help - Generics without specialization cannot be used as a type for a varia  (Read 193 times)

Jan_

  • Newbie
  • Posts: 4
i try to foreward generics:
Code: Pascal  [Select][+][-]
  1. program programm1;
  2.  
  3. uses fgl;
  4.  
  5. type
  6. generic  GDataList<T2> =  class
  7. public
  8.   item:T2;
  9. end;
  10.  
  11. generic Gbigclass <T1> =  class
  12. public
  13.   Glittleitem1:GDataList<T1>;
  14.   Glittleitem2:GDataList<T1>;
  15. end;
  16.  
  17. TBigclass = specialize Gbigclass<Integer>;
  18.  
  19.  
  20. var
  21.   MyClass: TBigclass;
  22. begin
  23.   MyClass := Gbigclass.Create;
  24. end.
  25.  
on line   "Glittleitem1:GDataList<T1>;" i get the Error:
(13,16) Error: Generics without specialization cannot be used as a type for a variable

is there a chance to get it late binded?

Greats Jan_

bytebites

  • Sr. Member
  • ****
  • Posts: 371
"Early binding"
Code: Pascal  [Select][+][-]
  1. Glittleitem1:specialize GDataList<T1>;

Jan_

  • Newbie
  • Posts: 4
whoooho, thanks.
Code: Pascal  [Select][+][-]
  1. program programm1;
  2.  
  3. uses sysutils;
  4.  
  5. type
  6. generic  GDataItem<T2> =  class
  7. public
  8.   item:T2;
  9.   Constructor create;
  10. end;
  11.  
  12. generic GBigClass<T1> =  class
  13. type
  14.   TDataItem=specialize GDataItem<T1>;
  15. public
  16.   item1:TDataItem;
  17.   item2:TDataItem;
  18.   Constructor create;
  19. end;
  20.  
  21. TBigClass = specialize GBigClass<Integer>;
  22.  
  23. Constructor GBigClass.create;
  24. begin
  25.   writeln('constructor GBigClass');
  26.   self.item1:=  TDataItem.create;
  27.   self.item2:=  TDataItem.create;
  28. end;
  29.  
  30. Constructor GDataItem.create;
  31. begin
  32.   writeln('constructor GDataItem');
  33. end;
  34.  
  35. var
  36.   MyClass: TBigClass;
  37. begin
  38.   MyClass := TBigClass.Create;
  39.  
  40.   MyClass.item1.item:=2;
  41.   MyClass.item2.item:=5;
  42.  
  43.   writeln(inttostr(MyClass.item1.item+MyClass.item2.item)); //2+5 = 7
  44.   readln();
  45. end.                    
« Last Edit: November 25, 2020, 12:41:59 pm by Jan_ »

 

TinyPortal © 2005-2018