[SOLVED]Is it possible that a function returns a function? lazarus / 64-FPC development ---------------------------------------------------------------------- BeanzMaster · 2020-02-16 10:16 Hi, to with Delphi a function can return a "type of function" like this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i InterfaceType  TCustomFunc = reference to function(AValue : Integer) : Integer;  TMyObject = class  public    Class function getAFunction(param1, param2 : Integer) : TCustomFunc;  end; implementation function TMyObject.getAFunction(param1, param2 : Integer) : TCustomFunc;begin if param1 < 100 then begin   Result := function(AValue : Integer): Integer             begin               Result := param1 + AValue * 10 - param2;             end;  end  else  begin   Result := function(AValue : Integer): Integer             begin               Result := (param2 * 10) + (AValue * 10) - param1;             end;  end;end; It is possible with actual stable Lazarus (2.0.6) and FPC (3.0.4) ? or it is possible with FPC 3.1.xx or up ? Thanks in advance Cheers ---------------------------------------------------------------------- Thaddy · 2020-02-16 12:02 Yes, that is possible. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}{$H+}type  TMyFunc = function(const value:Integer = 0):integer;  TAnotherFunc = function(const value:TMyFunc):TMyFunc;   function test1(const value: integer):Integer;  begin    writeln('test1 ',value);    Result := value;  end;    function test2(const value:TMyFunc):TMyFunc;  begin    writeln('through test2 ', value(test1(100)));    Result := Value;  end;  var  a:TMyFunc = test1;  b:TAnotherFunc = Test2;  begin  a:=b(a);  writeln(a(1));end. This outputs: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i through test2 test1 100test1 100100test1 11Note that reference to procedure/function and anonymous methods are not available (yet) for all platforms and modes (only Mac OSX), but the above code does the same. In the future this will work with your code too, though. I simply have the earlier, more complex syntax, but the effect is the same. ---------------------------------------------------------------------- BeanzMaster · 2020-02-16 12:38 Thanks Thaddy, i hope anonymous function come soon  I'll try to figure how to implement it in a good way in my class (for easing animation) ---------------------------------------------------------------------- howardpc · 2020-02-16 12:53 With Laz 2.0.6 and FPC 3.0.4 the following program Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program CustomFunction; {$mode objfpc}{$H+} type  TCustomFunc = function(AValue: Integer): Integer;   TMyObject = class  public    class var      p1, p2: Integer;    class function GetAFunction(param1, param2: Integer): TCustomFunc;  end;   function Param100OrMore(aValue: Integer): Integer;  begin    Result := (TMyObject.p2 * 10) + (AValue * 10) - TMyObject.p1;  end;   function Param100Less(aValue: Integer): Integer;  begin    Result := TMyObject.p1 + AValue * 10 - TMyObject.p2;  end; class function TMyObject.GetAFunction(param1, param2: Integer): TCustomFunc;begin  p1 := param1;  p2 := param2;  case (param1 < 100) of    True:  Exit(@Param100Less);    False: Exit(@Param100OrMore);  end;end; begin  WriteLn('param1 = 10,  param2 = 1, aValue = 2, result = ',TMyObject.GetAFunction(10, 1)(2));  WriteLn('param1 = 100, param2 = 1, aValue = 2, result = ',TMyObject.GetAFunction(100, 1)(2));end.outputs this: param1 = 10,  param2 = 1, aValue = 2, result = 29 param1 = 100, param2 = 1, aValue = 2, result = -70 ---------------------------------------------------------------------- BeanzMaster · 2020-02-16 13:26 Hi, thanks Howard is more what i'm search, btw i need to pass thrue (global/class variables) for having the behaviour, i want Best regards