Skip to content

TUnaryFunctor

Ivan Semenkov edited this page Jan 30, 2021 · 1 revision

Table of contents

About

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;
Example
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;
Clone this wiki locally