Recent

Author Topic: Help topic for 'Generics', template list  (Read 2508 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2725
    • UVviewsoft
Help topic for 'Generics', template list
« on: November 14, 2021, 01:39:02 am »
https://www.freepascal.org/docs-html/current/ref/refse53.html
This tells us that 'template list' may contain 'identifier : typeId ; identifier2 : tideId2'. But no examples for that! Examples only show very simple template list. Pls give an example for complex template list.

Warfley

  • Hero Member
  • *****
  • Posts: 2071
Re: Help topic for 'Generics', template list
« Reply #1 on: November 14, 2021, 01:58:21 am »
I think this mostly refers to the type constraints
Code: Pascal  [Select][+][-]
  1. generic TTest<T: TObject> =
this can only be a class (I.e. inherits from TObject)
The reason you have ; is probably because , is for having multiple of the same type like "A, B: TObject" which makes both A And B require to be a class vs "A; B: TObject" which makes A unrestricted and B a class

I think on the trunk there is also the feature for constants:
Code: Pascal  [Select][+][-]
  1. generic TBuffer<const Size: SizeInt> …
  2.  
  3. T1KBuff = specialize TBuffer<1024>
But I’m not sure right now if I remember correctly

But yes, this definitely needs some documentation, I figured that stuff out a year ago or so simply by trial and error, just trying out various constructs that I thought could work, and surprisingly many of them worked (at least on trunk)

AlexTP

  • Hero Member
  • *****
  • Posts: 2725
    • UVviewsoft
Re: Help topic for 'Generics', template list
« Reply #2 on: November 14, 2021, 03:26:56 am »
I found some explanations later, in the another Generics page.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Help topic for 'Generics', template list
« Reply #3 on: November 15, 2021, 01:20:44 am »

This is still a simple one, but I had a try for practise.
Code: Pascal  [Select][+][-]
  1.  
  2. {$mode delphi}
  3.  
  4. type udt<T>=class
  5. private
  6. procedure switch(var a, b: T);
  7. public
  8. arr:array of T;
  9. procedure bubblesort(n:int32);
  10. procedure print();
  11. procedure setup(n:int32);
  12. end;
  13.  
  14. procedure udt<T>.setup(n:int32);
  15. begin
  16.   setlength(arr,n);
  17. end;
  18.  
  19. procedure udt<T>.print();
  20. var i:int32;
  21. begin
  22. for i:=0 to high(arr) do write(arr[i],' ');
  23. writeln;
  24. end;
  25.  
  26. procedure udt<T>.switch(var a, b: T);
  27. var temp: T;
  28. begin
  29.   temp := a; a := b; b := temp;
  30. end;
  31.  
  32. procedure udt<T>.bubblesort(n:int32);
  33. var
  34. i:int32;
  35. begin
  36. if (n = 1) then exit;
  37.     for i:=0 to n-2 do if (arr[i] > arr[i+1]) then switch(arr[i],arr[i+1]);
  38.     bubbleSort(n-1);
  39. end;
  40.  
  41. var
  42. u:udt<integer>;
  43. v:udt<string>;
  44. i:int32;
  45.  
  46. begin
  47. u:=udt<integer>.create;
  48. u.setup(35);
  49. for i:=0 to high(u.arr) do u.arr[i]:=random(100);
  50. u.print;
  51. u.bubblesort(length(u.arr));
  52. u.print;
  53. writeln;
  54. //=========================//
  55. v:=udt<string>.create;
  56. v.setup(50);
  57. for i:=0 to high(v.arr) do v.arr[i]:=chr(65+random(90-64));
  58. v.print;
  59. v.bubblesort(length(v.arr));
  60. v.print;
  61. writeln('Press return to end . . .');
  62. readln;
  63. end.
  64.  
  65.  

 

TinyPortal © 2005-2018