"function reference" as a result of the function
I have a function that returns the TFunc<Integer> reference to function:Integer , which reference to function:Integer . and I have a procedure that takes the TFunc<Integer> function as an 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); ) leads to an error. and a call with () works as intended.
my question is:
when in delphi I need marriages to call a function, and when not (I thought I never need them)
or
Shouldn't I need them and is that a mistake?
I am working with delphi xe5 on windows 7 dcc32.
If what you are reporting is correct (and see below to learn more about this), you would find a mistake, I suppose.
This code:
FunctionCall(GetFunction); should not be compiled. 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 a type of type
function: TFunc<Integer> All of the above compilers with this error message:
[dcc32 Error] E2010 Incompatible types: 'System.SysUtils.TFunc' and 'Procedure'
So, if you somehow (possibly with some compiler options) managed to make this code a compilation, then I can only assume that this is due to an error.
You should deal with this by using parentheses so that the compiler can understand that you want to call GetFunction , rather than referring to it.
FunctionCall(GetFunction());