Reputation: 41
I need to pass a 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 Delphi programming code works. But with parameter modifications like const
, var
, or default values like...
function DoSomething(const AInput: Integer = 0): Integer;
...compiler returns an error of mismatch parameter list.
Is there any way to pass parameter modificators, or avoid this error?
Many thanks for your suggestions.
Upvotes: 3
Views: 964
Reputation: 700
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;
Upvotes: 5
Reputation: 47714
You can wrap it in an Anonymous Method like this:
SomeProc(function(Arg: Integer): Integer begin Result := DoSomething(Arg) end);
Upvotes: 7
Reputation: 612993
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>
.
Upvotes: 4