Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFunc<T> - is there any way to pass input parameter modificators?

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.

like image 613
Alexander Krylyk Avatar asked Dec 07 '25 06:12

Alexander Krylyk


2 Answers

You can wrap it in an Anonymous Method like this:

SomeProc(function(Arg: Integer): Integer begin Result := DoSomething(Arg) end);
like image 99
Uwe Raabe Avatar answered Dec 10 '25 02:12

Uwe Raabe


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;
like image 22
FredS Avatar answered Dec 10 '25 04:12

FredS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!