2
votes

I need to pass function as a parameter like this:

procedure SomeProc(AParameter: TFunc<Integer, Integer>);

When I have this function...

function DoSomething(AInput: Integer): Integer;
...
SomeProc(DoSomething);
...

...the code works. But with parameter modificators like const, var, or default values like...

function DoSomething(const AInput: Integer = 0): Integer;

...compiler returns error of mismatch parameter list.

Is there any way to pass parameter modificators, or avoid this error?

Many thanks for your suggestions.

3

3 Answers

5
votes

You can wrap it in an Anonymous Method like this:

SomeProc(function(Arg: Integer): Integer begin Result := DoSomething(Arg) end);
4
votes

Only if you declare it as a Method reference:

type TDoSomething = reference to function(const AInput: Integer = 0): Integer;

function SomeProc(AParameter: TDoSomething): Integer;
begin
  Result := AParameter;
end;

function CallSomeProc: integer;
begin
  Result := SomeProc(function(const AInput: Integer = 0): Integer begin Result := AInput end);
end;
3
votes

Is there any way to pass parameter modificators, or avoid this error?

No. The function you supply to SomeProc must have a signature that matches TFunc<Integer, Integer>.