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.
You can wrap it in an Anonymous Method like this:
SomeProc(function(Arg: Integer): Integer begin Result := DoSomething(Arg) end);
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With