Can TProc<T>
be made to allow aValue not just to be read, but to be edited in a TProc
anonymous procedure?
var xProc2: TProc<Integer>;
xProc2 := procedure(aValue: Integer)
begin
aValue := 5;
end;
Delphi 10.4.x
CodePudding user response:
If code requires exactly TProc<Integer>;
, then no. You can change local copy of value-type argument and use it inside procedure, but external code doesn't see changes.
If possible, you can use pointer argument of type PInteger
, and change aValue^
.
Also you can define own generic type with var
parameter
type
TProcvar<T> = reference to procedure (var Arg1: T);
and apply it
var xProc2: TProcVar<Integer>;
xProc2 := procedure(var aValue: Integer)
begin
aValue := 5;
end;
But the most reliable way, I think - use function TFunc<T,TResult>
( or TFunc<TResult>
if you don't use input argument)
var xFunc: TFunc<Integer, Integer>;
xfunc := function(aValue: Integer): Integer
begin
Result := aValue 5;
end;