Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"reference to function" as result of a function

i have a function that returns a function TFunc<Integer> which is reference to function:Integer. and i have a procedure which takes a function TFunc<Integer> as argument, calls it and prints its result.

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

function GetFunction:TFunc<Integer>;
begin
  result := function:Integer begin result := 42 end;
end;

procedure FunctionCall(AFunc:TFunc<Integer>);
var i:Integer;
begin
  i := AFunc;
  WriteLn(Format('Function Result = %d',[i]));
end;

begin
//  FunctionCall(GetFunction);    // error
  FunctionCall(GetFunction());  // works as excpected
end.

this call (FunctionCall(GetFunction);) results in an error. and the call with () works as excpected.

my question is:
when in delphi do i need brakets to call a function and when not (i thought that i never need them)
or
shouldn't i need them and is it a bug?

i work with delphi xe5 on windows 7 dcc32.

like image 996
linluk Avatar asked Sep 09 '14 13:09

linluk


1 Answers

If what you report is correct (and see below for more on that), then you would have found a bug, I believe.

This code:

FunctionCall(GetFunction);

should not compile. Indeed it does not compile when I try to compile it in XE3, XE4, XE5, XE6 and XE7. It does not compile because, in this particular context, the compiler interprets GetFunction as being of type

function: TFunc<Integer>

All above mentioned compilers object with this error message:

[dcc32 Error] E2010 Incompatible types: 'System.SysUtils.TFunc' and 'Procedure'

So, if you have somehow (perhaps with some compiler options), managed to make that code compile then I can only believe that is due to a bug.

You should deal with this by applying the parentheses so that the compiler can understand that you wish to call GetFunction, not refer to it.

FunctionCall(GetFunction());
like image 172
David Heffernan Avatar answered Sep 28 '22 20:09

David Heffernan