<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi,</p>
    <p>with standard generics in pascal there is currently no solution,
      but you can do it with a "poor man's template". They work with
      .inc files which contain the generic part of your code and they
      are then included in your specialization unit. Something like this
      for a generic sort - I use Delphi syntax !:</p>
    <p>file mySuperSort.inc:</p>
    <font face="monospace">procedure Run;</font>
    <pre><span class="linenos" data-line="3"></span><span class="nx">var n, i: Integer;
  swapped: Boolean
begin
  // this is the generic array like object you want to sort
  n</span> :<span class="o">=</span> Length(this);<span class="nx"></span>
  repeat
<span class="linenos" data-line="4"></span>    <span class="nx">swapped</span> <span class="o">:=</span> <span class="kc">false</span>
<span class="linenos" data-line="5"></span>    <span class="k">for</span> <span class="nx">i:</span><span class="o">=</span><span class="mf">0 to </span><span class="nx">n</span><span class="o">-</span><span class="mf">1 do begin
      // greater is you generic compare</span><span class="p"></span>
<span class="linenos" data-line="6"></span>      <span class="k">if</span> <span class="p">greater(</span><span class="nx">this</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>, this<span class="p">[</span><span class="nx">i</span><span class="o">+</span><span class="mf">1</span><span class="p">]) then begin</span>
<span class="linenos" data-line="7"></span>        <span class="nx">swap</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="nx">i</span><span class="o">+</span><span class="mf">1</span><span class="p">)</span>;
<span class="linenos" data-line="8"></span>        <span class="nx">swapped</span> <span class="o">:=</span> <span class="kc">true</span>;
<span class="linenos" data-line="9"></span>      end; 
<span class="linenos" data-line="10"></span>    end; 
<span class="linenos" data-line="11"></span>    <span class="nx">n</span> <span class="o">:=</span> <span class="nx">n</span><span class="o"> - </span><span class="mf">1;</span>
<span class="p">  until not </span><span class="p"></span><span class="nx">swapped</span>;
end;

</pre>
    <p>Now include it at the place where you need it. In most of the
      cases the easiest way is to create a small adapter record/object</p>
    <pre>type 
  TMySortAdapter = record
    this: TMyDataArray;
    descending: Boolean;
    procedure swap(var a, b: TMyDataType); inline; 
    function greater(a, b: TMyDataType); inline;
    procedure sort(data: TMyDataArray; ADescending:Boolean);
  end;

procedure TMySortAdapter.swap(var a, b: TMyDataType);
var t: TMyDataType;
begin
  t:= a;
  a:= b;
  b:= t;
end;

function TMySortAdapter.greater(a, b: TMyDataType);
begin
  if not descending then
    Result:= a > b // whatever you need for you compare
  else
    Result:= b > a
end;

procedure TMySortAdapter.sort(data: TMyDataArray; ADescending:Boolean);
{$I mySuperSort.inc}
begin
  this:= data;
  descending:= ADescending;
  run;
end;

</pre>
    <p>That's the idea behind "poor man templates". Code completely
      untested <br>
    </p>
    <p>Not exactly the answer you were looking for, but it works.</p>
    <p>Adrian.<br>
    </p>
    <p></p>
    <div class="moz-cite-prefix">Am 14.11.22 um 19:26 schrieb Vojtěch
      Čihák via fpc-pascal:<br>
    </div>
    <blockquote type="cite" cite="mid:20221114192617.59B42508@atlas.cz">
      <pre class="moz-quote-pre" wrap="">Hi,
 
I wrote a generic abstract class - a list based on dynamic array (i.e. array of T;) and this class can be specialized elsewhere with any type (records or classes).
Part of the class is sorting. There are more ways how to deliver *compare function* to sorting method. I can pass it as a parameter or I can define it as: function Compare(A, B: T): Integer; virtual; abstract;. But this way the function cannot be inlined.
 
Question: Is there a way how to *inline* compare function to sorting method in this general purpose generic abstract class?
 
Thanks.
 
PS: The gain is 6-7%.  
_______________________________________________
fpc-pascal maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>
</pre>
    </blockquote>
  </body>
</html>