Recent

Author Topic: [SOLVED] Pass function as variable  (Read 2620 times)

aradeonas

  • Hero Member
  • *****
  • Posts: 824
[SOLVED] Pass function as variable
« on: October 20, 2014, 01:28:05 pm »
Hi,
Is there any way to do this in FreePascal like Delphi( and I don't want to use prefix @)?
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
  TCalcFunction = function(V1,V2:integer):integer;
var
  Form1: TForm1;

implementation

{$R *.lfm}
function SumFunction(V1,V2:integer):integer;
begin
  Result:=V1+V2;
end;
function Calculator(CalcFunction:TCalcFunction;V1,V2:integer):integer;
begin
 CalcFunction(V1,V2);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(Calculator(@SumFunction,2,3)));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(IntToStr(Calculator(
  function SumFunction(V1,V2:integer):integer
  begin
       Result:=V1+V2;
  end
    ,1,2)));
end;

end.

« Last Edit: October 20, 2014, 07:47:13 pm by aradeonas »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Pass function as variable
« Reply #1 on: October 20, 2014, 01:34:22 pm »
change
Code: [Select]
{$mode objfpc}{$H+}

to

Code: [Select]
{$mode delphi}

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Pass function as variable
« Reply #2 on: October 20, 2014, 01:36:32 pm »
I did that before and there is an error:
Code: [Select]
unit1.pas(50,3) Error: Illegal expression

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Pass function as variable
« Reply #3 on: October 20, 2014, 01:37:37 pm »
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}
change the above to
Code: [Select]
{$mode DELPHI}{$H+}

That will eliminate the need to use @.

As for the anonymous method as far as I know fpc does not support them (yet?). You need to make it an internal function something like
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
  function SumInternalFunction(V1,V2:integer):integer
  begin
       Result:=V1+V2;
  end
begin
  ShowMessage(IntToStr(Calculator(SumInternalFunction ,1,2)));
end;

should do the trick for you.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Pass function as variable
« Reply #4 on: October 20, 2014, 01:41:49 pm »
Thank you and I know that but in my situation I need to write cod like that and I want to sure there is no support for this in FreePascal.
http://forum.lazarus.freepascal.org/index.php/topic,26193.0.html
@marcov is there or not?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Pass function as variable
« Reply #5 on: October 20, 2014, 02:01:28 pm »
Indeed, no anonymous methods yet.  {$mode delphi} is mostly targeted at D7, and isn't compatible with e.g. the unicode side of D2009+ either.


Taaz: {$mode delphi} implies {$H+}

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Pass function as variable
« Reply #6 on: October 20, 2014, 02:06:59 pm »
Thank you.

 

TinyPortal © 2005-2018