Sokol, I'm writing a function GenArray(generator) that returns a random array populated by calling the generator function. So the return type of GenArray matches "array of" the return type of the generator function, for any generator function.<br clear="all">

<div><br></div><div>E.g., GenArray(GenChar) would return a random string. (Except I don't know the syntax for passing function pointers.)</div><div><br></div><div>Maybe you could fork my code and use templates to achieve this?</div>

<div><br></div><div><a href="https://github.com/mcandre/paycheck">GitHub</a></div><div><br></div>Cheers,<div><br></div><div>Andrew Pennebaker</div><div><a href="http://www.yellosoft.us" target="_blank">www.yellosoft.us</a></div>


<br><div class="gmail_quote">On Tue, Oct 18, 2011 at 4:57 AM, Lukasz Sokol <span dir="ltr"><<a href="mailto:el.es.cr@gmail.com">el.es.cr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div><div></div><div class="h5">On 17/10/2011 21:53, Andrew Pennebaker wrote:<br>
> Does Free Pascal have anonymous functions that you can pass around,<br>
> e.g. to a sort(compare : function, arr : array) function?<br>
><br>
> If not, does anyone know any hacks to accomplish this?<br>
><br>
> Cheers,<br>
><br>
</div></div>> Andrew Pennebaker <a href="http://www.yellosoft.us" target="_blank">www.yellosoft.us</a> <<a href="http://www.yellosoft.us" target="_blank">http://www.yellosoft.us</a>><br>
><br>
<br>
Yes:<br>
<br>
//you can declare a type :<br>
<br>
TMyFunction: function(AMyParam : TType): TOtherType;<br>
<br>
// then implement:<br>
<br>
function MyFunctionWithArbitraryName(AMyParam: TType): TOtherType;<br>
begin<br>
  {do something with AMyParam}<br>
end;<br>
<br>
//then (or before, if the TMyFunction is in type declaration section)<br>
<br>
function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction):TWhateverType;<br>
var OtherReturn: TOtherType;<br>
    AParameterToAFunction : TType;<br>
begin<br>
  {some code}<br>
  OtherReturn := AFunction(AParameter);<br>
  {other code}<br>
end;<br>
<br>
// and finally even<br>
const   MYFUNCTIONCOUNT = 1<br>
        ArrayOfTMyFunction = array[0..MYFUNCTIONCOUNT-1] of TMyFunction = (MyFunctionWithArbitraryName);<br>
                                                {it's a static array, so match the number of elemets}<br>
<br>
begin<br>
  for {declared somewhere global} i := 0 to MYFUNCTIONCOUNT-1 do<br>
    WhateverReturn := AFunctionUsingTMyFunctionAsArgument(ArrayOfTMyFunction[i](MyParam));<br>
<br>
end.<br>
<br>
(Terms and conditions : this is invoked from /dev/mem, some syntax discrepancies may occur<br>
as my attention moved on from this as it obviously worked)<br>
<br>
I have written a dumb CLI interpreter this way ;) recently.<br>
(the function table contains command name as string in a record together with the function<br>
and the main procedure looks for the name and executes the arbitrary function when found)<br>
<br>
TMyFunctionRecord = record<br>
  CLIName: string;<br>
  MyFunction : TMyFunction<br>
end;<br>
<br>
const ArrayOfCLIFunctions = array[0..CLIFUNCCOUNT-1] of TMyFunctionRecord = ({...});<br>
<br>
var CLIFunctionToExecute : TMyFunction;<br>
    WhateverReturn : TWhateverType;<br>
<br>
begin<br>
  for i := 0 to CLIFUNCCOUNT-1 do<br>
    if ArrayOfCLIFunctions[i].CLIName = paramstr[1] then<br>
      begin<br>
        CLIFunctionToExecute := ArrayOfCLIFunctions[i].MyFunction;<br>
        break;<br>
      end;<br>
  WhateverReturn := AFunctionUsingTMyFunctionAsArgument(CLIFunctionToExecute(MyParam));<br>
end.<br>
<br>
<br>
Also the 'anonymous' functions can be implemented in a separate unit which only exports the<br>
/relevant/ ones in its interface section.<br>
<br>
The obvious limitation / safeguard is : you must use the function of declared type to pass into<br>
the function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction) and no other type;<br>
(An obvious workaround to that is to use varargs ;) but I did not try that so I can't tell<br>
whether that would work)<br>
<br>
It's not much OOP in action (and may have {obvious for some //not me} performance penalties but oh well. ;)<br>
<font color="#888888"><br>
L.<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
fpc-pascal maillist  -  <a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a><br>
</div></div></blockquote></div><br>