This repository has been archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
TUnaryFunctor
Ivan Semenkov edited this page Jan 30, 2021
·
1 revision
Generically, functor objects are instances of a class with member function Call
defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore its type can be used as template parameter when a generic function type is expected.
In the case of unary functor objects, this Call
member function takes a single parameter.
uses
utils.functor;
type
generic TUnaryFunctor<V, R> = class
public
function Call(AValue : V) : R; abstract;
end;
uses
utils.functor;
type
TFunctor = {$IFDEF FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>
public
function Call(AValue : Integer) : Integer; override;
begin
if AValue < 0 then
Exit(-1)
else if AValue > 0 then
Exit(1)
else
Exit(0);
end;
end;
var
functor : TFunctor;
begin
functor := TFunctor.Create;
if functor.Call(21) < 0 then
;
FreeAndNil(functor);
end;