Recent

Author Topic: [SOLVED]Is it possible that a function returns a function?  (Read 1537 times)

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
[SOLVED]Is it possible that a function returns a function?
« on: February 16, 2020, 11:16:53 am »
Hi, to with Delphi a function can return a "type of function" like this :

Code: Pascal  [Select][+][-]
  1. Interface
  2. Type
  3.   TCustomFunc = reference to function(AValue : Integer) : Integer;
  4.   TMyObject = class
  5.   public
  6.     Class function getAFunction(param1, param2 : Integer) : TCustomFunc;
  7.   end;
  8.  
  9. implementation
  10.  
  11. function TMyObject.getAFunction(param1, param2 : Integer) : TCustomFunc;
  12. begin
  13.  if param1 < 100 then
  14.  begin
  15.    Result := function(AValue : Integer): Integer
  16.              begin
  17.                Result := param1 + AValue * 10 - param2;
  18.              end;
  19.   end
  20.   else
  21.   begin
  22.    Result := function(AValue : Integer): Integer
  23.              begin
  24.                Result := (param2 * 10) + (AValue * 10) - param1;
  25.              end;
  26.   end;
  27. 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
« Last Edit: February 16, 2020, 02:26:39 pm by BeanzMaster »

Thaddy

  • Hero Member
  • *****
  • Posts: 14215
  • Probably until I exterminate Putin.
Re: Is it possible that a function returns a function?
« Reply #1 on: February 16, 2020, 01:02:53 pm »
Yes, that is possible.
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. type
  3.   TMyFunc = function(const value:Integer = 0):integer;
  4.   TAnotherFunc = function(const value:TMyFunc):TMyFunc;
  5.  
  6.   function test1(const value: integer):Integer;
  7.   begin
  8.     writeln('test1 ',value);
  9.     Result := value;
  10.   end;
  11.  
  12.   function test2(const value:TMyFunc):TMyFunc;
  13.   begin
  14.     writeln('through test2 ', value(test1(100)));
  15.     Result := Value;
  16.   end;
  17.  
  18. var
  19.   a:TMyFunc = test1;
  20.   b:TAnotherFunc = Test2;  
  21. begin
  22.   a:=b(a);
  23.   writeln(a(1));
  24. end.

This outputs:
Code: Text  [Select][+][-]
  1. through test2 test1 100
  2. test1 100
  3. 100
  4. test1 1
  5. 1
Note 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.
« Last Edit: February 16, 2020, 01:15:18 pm by Thaddy »
Specialize a type, not a var.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: Is it possible that a function returns a function?
« Reply #2 on: February 16, 2020, 01:38:19 pm »
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

  • Hero Member
  • *****
  • Posts: 4144
Re: Is it possible that a function returns a function?
« Reply #3 on: February 16, 2020, 01:53:46 pm »
With Laz 2.0.6 and FPC 3.0.4 the following program
Code: Pascal  [Select][+][-]
  1. program CustomFunction;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type
  6.   TCustomFunc = function(AValue: Integer): Integer;
  7.  
  8.   TMyObject = class
  9.   public
  10.     class var
  11.       p1, p2: Integer;
  12.     class function GetAFunction(param1, param2: Integer): TCustomFunc;
  13.   end;
  14.  
  15.   function Param100OrMore(aValue: Integer): Integer;
  16.   begin
  17.     Result := (TMyObject.p2 * 10) + (AValue * 10) - TMyObject.p1;
  18.   end;
  19.  
  20.   function Param100Less(aValue: Integer): Integer;
  21.   begin
  22.     Result := TMyObject.p1 + AValue * 10 - TMyObject.p2;
  23.   end;
  24.  
  25. class function TMyObject.GetAFunction(param1, param2: Integer): TCustomFunc;
  26. begin
  27.   p1 := param1;
  28.   p2 := param2;
  29.   case (param1 < 100) of
  30.     True:  Exit(@Param100Less);
  31.     False: Exit(@Param100OrMore);
  32.   end;
  33. end;
  34.  
  35. begin
  36.   WriteLn('param1 = 10,  param2 = 1, aValue = 2, result = ',TMyObject.GetAFunction(10, 1)(2));
  37.   WriteLn('param1 = 100, param2 = 1, aValue = 2, result = ',TMyObject.GetAFunction(100, 1)(2));
  38. end.
outputs this:
param1 = 10,  param2 = 1, aValue = 2, result = 29
param1 = 100, param2 = 1, aValue = 2, result = -70

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: Is it possible that a function returns a function?
« Reply #4 on: February 16, 2020, 02:26:08 pm »
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

 

TinyPortal © 2005-2018