Advertisement
sglienke

Generic InRange function

Jun 1st, 2016
210
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program Test;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Generics.Defaults;
  7.  
  8. type
  9.   Op = record
  10.   strict private type
  11.     Comparer<T> = record
  12.       class var comparer: IComparer<T>;
  13.       class constructor Create;
  14.     end;
  15.   public
  16.     class function InRange<T>(const AValue, AMin, AMax: T): Boolean; inline; static;
  17.   end;
  18.  
  19. class constructor Op.Comparer<T>.Create;
  20. begin
  21.   comparer := TComparer<T>.Default;
  22. end;
  23.  
  24. class function Op.InRange<T>(const AValue, AMin, AMax: T): Boolean;
  25. begin
  26.   Result := (Comparer<T>.comparer.Compare(AValue, AMin) >= 0)
  27.     and (Comparer<T>.comparer.Compare(AValue, AMax) <= 0);
  28. end;
  29.  
  30. begin
  31.   WriteLn(Op.InRange(0.5, 0.1, 1.0));
  32. end.
Advertisement
RAW Paste Data Copied
Advertisement