unit pixel_classes;

interface

uses
  System.SysUtils, System.Variants, Rtti, TypInfo;

{$TYPEINFO ON}

type
  TZString = type string;

  TNullValue = record
  end;

  // Forward declaration for constraint types
  IFloatValue = interface
    ['{A5F7D8C9-B1E2-4F3D-9A0B-8C7D6E5F4A3B}']
  end;

  IIntegerValue = interface
    ['{E9D8C7B6-A5F4-3E2D-1B0A-9F8E7D6C5B4A}']
  end;

  // Helper records to implement constraints
  TFloatValueHelper = record
  private
    Value: Extended;
  public
    class operator Implicit(const AValue: Currency): TFloatValueHelper;
    class operator Implicit(const AValue: Double): TFloatValueHelper;
    class operator Implicit(const AValue: Extended): TFloatValueHelper;
    class operator Implicit(const AValue: Single): TFloatValueHelper;
  end;

  TIntegerValueHelper = record
  private
    Value: Int64;
  public
    class operator Implicit(const AValue: Integer): TIntegerValueHelper;
    class operator Implicit(const AValue: Int64): TIntegerValueHelper;
    class operator Implicit(const AValue: SmallInt): TIntegerValueHelper;
    class operator Implicit(const AValue: ShortInt): TIntegerValueHelper;
    class operator Implicit(const AValue: Byte): TIntegerValueHelper;
    class operator Implicit(const AValue: Word): TIntegerValueHelper;
    class operator Implicit(const AValue: Cardinal): TIntegerValueHelper;
  end;

  // The main TNullable record with type constraints
  TNullable<T> = record
  private
    class function DoAdd(const A, B: T): T; static;
    class function DoSubtract(const A, B: T): T; static;
    class function DoMultiply(const A, B: T): T; static;
    class function DoDivide(const A, B: T): T; static;
    class function DoDivInt(const A, B: T): T; static;
  public
    Value: T;
    HasValue: Boolean;

    class operator Implicit(const AValue: T): TNullable<T>;
    class operator Implicit(const AValue: TNullValue): TNullable<T>;
    class operator Explicit(const AValue: T): TNullable<T>;
    class operator Equal(const A: TNullable<T>; const B: TNullValue): Boolean;
    class operator NotEqual(const A: TNullable<T>; const B: TNullValue): Boolean;
    class operator Implicit(const A: TNullable<T>): T;

    // Basic arithmetic operators
    class operator Add(const A: TNullable<T>; const B: T): TNullable<T>;
    class operator Subtract(const A: TNullable<T>; const B: T): TNullable<T>;
    class operator Multiply(const A: TNullable<T>; const B: T): TNullable<T>;

    // Division operators constrained by types
    class operator IntDivide(const A: TNullable<TIntegerValueHelper>; const B: TIntegerValueHelper): TNullable<TIntegerValueHelper>; inline;
    class operator Divide(const A: TNullable<TFloatValueHelper>; const B: TFloatValueHelper): TNullable<TFloatValueHelper>; inline;
  end;

var NullValue: TNullValue;

implementation

{ TFloatValueHelper }

class operator TFloatValueHelper.Implicit(const AValue: Currency): TFloatValueHelper;
begin
  Result.Value := AValue;
end;

class operator TFloatValueHelper.Implicit(const AValue: Double): TFloatValueHelper;
begin
  Result.Value := AValue;
end;

class operator TFloatValueHelper.Implicit(const AValue: Extended): TFloatValueHelper;
begin
  Result.Value := AValue;
end;

class operator TFloatValueHelper.Implicit(const AValue: Single): TFloatValueHelper;
begin
  Result.Value := AValue;
end;

{ TIntegerValueHelper }

class operator TIntegerValueHelper.Implicit(const AValue: Integer): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: Int64): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: SmallInt): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: ShortInt): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: Byte): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: Word): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

class operator TIntegerValueHelper.Implicit(const AValue: Cardinal): TIntegerValueHelper;
begin
  Result.Value := AValue;
end;

{ TNullable<T> }

class operator TNullable<T>.Implicit(const AValue: T): TNullable<T>;
begin
  Result.Value := AValue;
  Result.HasValue := True;
end;

class operator TNullable<T>.Implicit(const AValue: TNullValue): TNullable<T>;
begin
  Result.Value := Default(T);
  Result.HasValue := False;
end;

class operator TNullable<T>.Explicit(const AValue: T): TNullable<T>;
begin
  Result.Value := AValue;
  Result.HasValue := True;
end;

class operator TNullable<T>.Equal(const A: TNullable<T>; const B: TNullValue): Boolean;
begin
  Result := not A.HasValue;
end;

class operator TNullable<T>.NotEqual(const A: TNullable<T>; const B: TNullValue): Boolean;
begin
  Result := A.HasValue;
end;

class operator TNullable<T>.Implicit(const A: TNullable<T>): T;
begin
  if A.HasValue then
    Result := A.Value
  else
    Result := Default(T);
end;

class function TNullable<T>.DoAdd(const A, B: T): T;
var
  VA, VB: Variant;
begin
  VA := TValue.From<T>(A).AsVariant;
  VB := TValue.From<T>(B).AsVariant;
  Result := TValue.FromVariant(VA + VB).AsType<T>;
end;

class function TNullable<T>.DoSubtract(const A, B: T): T;
var
  VA, VB: Variant;
begin
  VA := TValue.From<T>(A).AsVariant;
  VB := TValue.From<T>(B).AsVariant;
  Result := TValue.FromVariant(VA - VB).AsType<T>;
end;

class function TNullable<T>.DoMultiply(const A, B: T): T;
var
  VA, VB: Variant;
begin
  VA := TValue.From<T>(A).AsVariant;
  VB := TValue.From<T>(B).AsVariant;
  Result := TValue.FromVariant(VA * VB).AsType<T>;
end;

class function TNullable<T>.DoDivide(const A, B: T): T;
var
  VA, VB: Variant;
begin
  VA := TValue.From<T>(A).AsVariant;
  VB := TValue.From<T>(B).AsVariant;
  Result := TValue.FromVariant(VA / VB).AsType<T>;
end;

class function TNullable<T>.DoDivInt(const A, B: T): T;
var
  VA, VB: Variant;
begin
  VA := TValue.From<T>(A).AsVariant;
  VB := TValue.From<T>(B).AsVariant;
  Result := TValue.FromVariant(VA div VB).AsType<T>;
end;

class operator TNullable<T>.Add(const A: TNullable<T>; const B: T): TNullable<T>;
begin
  if not A.HasValue then
    Exit(NullValue);
  Result.HasValue := True;
  Result.Value := DoAdd(A.Value, B);
end;

class operator TNullable<T>.Subtract(const A: TNullable<T>; const B: T): TNullable<T>;
begin
  if not A.HasValue then
    Exit(NullValue);
  Result.HasValue := True;
  Result.Value := DoSubtract(A.Value, B);
end;

class operator TNullable<T>.Multiply(const A: TNullable<T>; const B: T): TNullable<T>;
begin
  if not A.HasValue then
    Exit(NullValue);
  Result.HasValue := True;
  Result.Value := DoMultiply(A.Value, B);
end;

class operator TNullable<T>.IntDivide(const A: TNullable<TIntegerValueHelper>; const B: TIntegerValueHelper): TNullable<TIntegerValueHelper>;
begin
  if not A.HasValue then
    Exit(NullValue);
  Result.HasValue := True;
  Result.Value := DoDivInt(A.Value, B);
end;

class operator TNullable<T>.Divide(const A: TNullable<TFloatValueHelper>; const B: TFloatValueHelper): TNullable<TFloatValueHelper>;
begin
  if not A.HasValue then
    Exit(NullValue);
  Result.HasValue := True;
  Result.Value := DoDivide(A.Value, B);
end;

end.

The are some compiling errors:

[dcc32 Error] pixel_classes.pas(74): E2518 Operator 'IntDivide' must take least one 'TNullable<T>' type in parameters
[dcc32 Error] pixel_classes.pas(75): E2518 Operator 'Divide' must take least one 'TNullable<T>' type in parameters
[dcc32 Error] pixel_classes.pas(253): E2010 Incompatible types: 'T' and 'TIntegerValueHelper'
[dcc32 Error] pixel_classes.pas(261): E2010 Incompatible types: 'T' and 'TFloatValueHelper'

Review this unit and fix it