Muy bien saludos a todos los del foro, hoy inicio mis labores haciendo esta pequeña publicación con respecto a la sobrecarga de operadores para los tipos de datos más comunes en Delphi, a continuación expongo mi pequeña librería, espero sus comentarios y si a alguien le es de utilidad este mi aporte a la comunidad espero me lo hagan saber.
unit TiposDeDatos; interface uses SysUtils, Variants, Classes, StdCtrls, Generics.Collections,JclHashMaps, JclContainerIntf; resourcestring sCantConvertNil = 'No puede contener nil'; sOnlyValidValueIsNil = ' El único valor válido es nil '; sOnlyValidOperands = ' Los operandos no pueden ser nil '; sDivideByZero = ' No se puede realizar una división en 0 '; type WInteger = record private FValue: Integer; FIsNil: Boolean; public class operator Implicit(const Value: Integer): WInteger; class operator Implicit(Value: Pointer): WInteger; class operator Implicit(const Value: WInteger): Integer; class operator Add( op1, op2 : WInteger ) : WInteger; class operator Subtract( op1, op2 : WInteger ) : WInteger; class operator Multiply( op1, op2 : WInteger ) : WInteger; class operator Divide( op1, op2 : WInteger ) : WInteger; class operator LessThan( op1, op2 : WInteger ) : boolean; class operator GreaterThan( op1, op2 : WInteger ) : boolean; class operator NotEqual( op1, op2 : WInteger ) : boolean; class operator Equal( op1, op2 : WInteger ) : boolean; class operator GreaterThanOrEqual( op1, op2 : WInteger ) : boolean; class operator LessThanOrEqual( op1, op2 : WInteger ) : boolean; class operator Negative( op1 : WInteger ) : WInteger; function ToStr( ): String; property IsNil: Boolean read FIsNil; property Value: Integer read FValue; end; WDouble = record private FValue: Double; FIsNil: Boolean; public class operator Implicit(const Value: Double): WDouble; class operator Implicit(Value: Pointer): WDouble; class operator Implicit(const Value: WDouble): Double; class operator Add( op1, op2 : WDouble ) : WDouble; class operator Subtract( op1, op2 : WDouble ) : WDouble; class operator Multiply( op1, op2 : WDouble ) : WDouble; class operator Divide( op1, op2 : WDouble ) : WDouble; class operator LessThan( op1, op2 : WDouble ) : boolean; class operator GreaterThan( op1, op2 : WDouble ) : boolean; class operator NotEqual( op1, op2 : WDouble ) : boolean; class operator Equal( op1, op2 : WDouble ) : boolean; class operator GreaterThanOrEqual( op1, op2 : WDouble ) : boolean; class operator LessThanOrEqual( op1, op2 : WDouble ) : boolean; class operator Negative( op1 : WDouble ) : WDouble; function ToStr( ): String; property IsNil: Boolean read FIsNil; property Value: Double read FValue; end; WChar = record private FValue: Char; FIsNil: Boolean; public class operator Implicit(const Value: Char): WChar; class operator Implicit(Value: Pointer): WChar; class operator Implicit(const Value: WChar): Char; class operator Add( op1, op2 : WChar ) : String; class operator Multiply( op1: WChar; op2 : Integer ) : String; class operator LessThan( op1, op2 : WChar ) : boolean; class operator GreaterThan( op1, op2 : WChar ) : boolean; class operator NotEqual( op1, op2 : WChar ) : boolean; class operator Equal( op1, op2 : WChar ) : boolean; class operator GreaterThanOrEqual( op1, op2 : WChar ) : boolean; class operator LessThanOrEqual( op1, op2 : WChar ) : boolean; function quotedString(): String; property IsNil: Boolean read FIsNil; property Value: Char read FValue; end; WString = record private FValue: String; FIsNil: Boolean; public class operator Implicit(const Value: String): WString; class operator Implicit(Value: Pointer): WString; class operator Implicit(const Value: WString): String; class operator Add( op1, op2 : WString ) : String; class operator Multiply( op1: WString; op2 : Integer ) : String; class operator LessThan( op1, op2 : WString ) : boolean; class operator GreaterThan( op1, op2 : WString ) : boolean; class operator NotEqual( op1, op2 : WString ) : boolean; class operator Equal( op1, op2 : WString ) : boolean; class operator GreaterThanOrEqual( op1, op2 : WString ) : boolean; class operator LessThanOrEqual( op1, op2 : WString ) : boolean; function replace( const oldPattern : WString ; const newPattern: WString ): WString; function size( ): WInteger; function isEmpty() : boolean; function quotedString(): WString; function lastDelimiterOf( s : String ):Integer; function deleteChars( index : Integer ; n : Integer ): WString; property IsNil: Boolean read FIsNil; property Value: String read FValue; end; WDateTime = record private FValue: TDateTime; FIsNil: Boolean; public class operator Implicit(const Value: TDateTime): WDateTime; class operator Implicit(Value: Pointer): WDateTime; class operator Implicit(const Value: WDateTime): TDateTime; class operator Add( op1, op2 : WDateTime ) : WDateTime; class operator Subtract( op1, op2 : WDateTime ) : WDateTime; class operator Multiply( op1, op2 : WDateTime ) : WDateTime; class operator Divide( op1, op2 : WDateTime ) : WDateTime; class operator LessThan( op1, op2 : WDateTime ) : boolean; class operator GreaterThan( op1, op2 : WDateTime ) : boolean; class operator NotEqual( op1, op2 : WDateTime ) : boolean; class operator Equal( op1, op2 : WDateTime ) : boolean; class operator GreaterThanOrEqual( op1, op2 : WDateTime ) : boolean; class operator LessThanOrEqual( op1, op2 : WDateTime ) : boolean; class operator Negative( op1 : WDateTime ) : WDateTime; function ToStr( ): String; overload; function ToStr( pattern : String ): String; overload; property IsNil: Boolean read FIsNil; property Value: TDateTime read FValue; end; implementation class operator WInteger.Implicit(const Value: Integer): WInteger; begin Result.FValue := Value; Result.FIsNil := False; end; class operator WInteger.Implicit(Value: Pointer): WInteger; begin Assert(Value = nil, sOnlyValidValueIsNil); Result.FIsNil := True; end; class operator WInteger.Implicit(const Value: WInteger): Integer; begin if Value.IsNil then raise EConvertError.Create(sCantConvertNil); Result := Value.FValue; end; class operator WInteger.Add( op1, op2 : WInteger ) : WInteger; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue + op2.FValue; end; class operator WInteger.Subtract( op1, op2 : WInteger ) : WInteger; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue - op2.FValue; end; class operator WInteger.Multiply( op1, op2 : WInteger ) : WInteger; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue * op2.FValue; end; class operator WInteger.Divide( op1, op2 : WInteger ) : WInteger; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue div op2.FValue; end; class operator WInteger.LessThan( op1, op2 : WInteger ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue < op2.FValue; end; class operator WInteger.GreaterThan( op1, op2 : WInteger ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue > op2.FValue; end; class operator WInteger.LessThanOrEqual( op1, op2 : WInteger ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue <= op2.FValue; end; class operator WInteger.GreaterThanOrEqual( op1, op2 : WInteger ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue >= op2.FValue; end; class operator WInteger.Equal( op1, op2 : WInteger ) : boolean; begin if op1.IsNil and op2.IsNil then Result := True else if op1.IsNil or op2.IsNil then Result := False else Result := op1.FValue = op2.FValue; end; class operator WInteger.NotEqual( op1, op2 : WInteger ) : boolean; begin if op1.IsNil and op2.IsNil then Result := False else if op1.IsNil or op2.IsNil then Result := True else Result := op1.FValue <> op2.FValue; end; class operator WInteger.Negative( op1 : WInteger ) : WInteger; begin if op1.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := - op1.FValue; end; function WInteger.toStr() : String; begin if self.IsNil then Result := '' else Result := InttoStr(self.FValue); end; class operator WDouble.Implicit(const Value: Double): WDouble; begin Result.FValue := Value; Result.FIsNil := False; end; class operator WDouble.Implicit(Value: Pointer): WDouble; begin Assert(Value = nil, sOnlyValidValueIsNil); Result.FIsNil := True; end; class operator WDouble.Implicit(const Value: WDouble): Double; begin if Value.IsNil then raise EConvertError.Create(sCantConvertNil); Result := Value.FValue; end; class operator WDouble.Add( op1, op2 : WDouble ) : WDouble; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue + op2.FValue; end; class operator WDouble.Subtract( op1, op2 : WDouble ) : WDouble; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue - op2.FValue; end; class operator WDouble.Multiply( op1, op2 : WDouble ) : WDouble; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue * op2.FValue; end; class operator WDouble.Divide( op1, op2 : WDouble ) : WDouble; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue / op2.FValue; end; class operator WDouble.LessThan( op1, op2 : WDouble ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue < op2.FValue; end; class operator WDouble.GreaterThan( op1, op2 : WDouble ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue > op2.FValue; end; class operator WDouble.LessThanOrEqual( op1, op2 : WDouble ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue <= op2.FValue; end; class operator WDouble.GreaterThanOrEqual( op1, op2 : WDouble ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue >= op2.FValue; end; class operator WDouble.Equal( op1, op2 : WDouble ) : boolean; begin if op1.IsNil and op2.IsNil then Result := True else if op1.IsNil or op2.IsNil then Result := False else Result := op1.FValue = op2.FValue; end; class operator WDouble.NotEqual( op1, op2 : WDouble ) : boolean; begin if op1.IsNil and op2.IsNil then Result := False else if op1.IsNil or op2.IsNil then Result := True else Result := op1.FValue <> op2.FValue; end; class operator WDouble.Negative( op1 : WDouble ) : WDouble; begin if op1.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := - op1.FValue; end; function WDouble.toStr() : String; begin if self.IsNil then Result := '' else Result := FloattoStr(self.FValue); end; class operator WChar.Implicit(const Value: Char): WChar; begin Result.FValue := Value; Result.FIsNil := False; end; class operator WChar.Implicit(Value: Pointer): WChar; begin Assert(Value = nil, sOnlyValidValueIsNil); Result.FIsNil := True; end; class operator WChar.Implicit(const Value: WChar): Char; begin if Value.IsNil then raise EConvertError.Create(sCantConvertNil); Result := Value.FValue; end; class operator WChar.Add( op1, op2 : WChar ) : String; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue + op2.FValue; end; class operator WChar.Multiply( op1: WChar ; op2 : Integer ) : String; var I: Integer; temp : String; begin if op1.IsNil then raise EConvertError.Create(sOnlyValidOperands); temp := ''; for I := 0 to op2 - 1 do temp := temp + op1.FValue; Result := temp; end; class operator WChar.LessThan( op1, op2 : WChar ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue < op2.FValue; end; class operator WChar.GreaterThan( op1, op2 : WChar ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue > op2.FValue; end; class operator WChar.LessThanOrEqual( op1, op2 : WChar ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue <= op2.FValue; end; class operator WChar.GreaterThanOrEqual( op1, op2 : WChar ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue >= op2.FValue; end; class operator WChar.Equal( op1, op2 : WChar ) : boolean; begin if op1.IsNil and op2.IsNil then Result := True else if op1.IsNil or op2.IsNil then Result := False else Result := op1.FValue = op2.FValue; end; class operator WChar.NotEqual( op1, op2 : WChar ) : boolean; begin if op1.IsNil and op2.IsNil then Result := False else if op1.IsNil or op2.IsNil then Result := True else Result := op1.FValue <> op2.FValue; end; function WChar.quotedString(): String; begin Result := QuotedStr( FValue ); end; class operator WString.Implicit(const Value: String): WString; begin Result.FValue := Value; Result.FIsNil := False; end; class operator WString.Implicit(Value: Pointer): WString; begin Assert(Value = nil, sOnlyValidValueIsNil); Result.FIsNil := True; end; class operator WString.Implicit(const Value: WString): String; begin if Value.IsNil then raise EConvertError.Create(sCantConvertNil); Result := Value.FValue; end; class operator WString.Add( op1, op2 : WString ) : String; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue + op2.FValue; end; class operator WString.Multiply( op1: WString ; op2 : Integer ) : String; var I: Integer; temp : String; begin if op1.IsNil then raise EConvertError.Create(sOnlyValidOperands); temp := ''; for I := 0 to op2 - 1 do temp := temp + op1.FValue; Result := temp; end; class operator WString.LessThan( op1, op2 : WString ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue < op2.FValue; end; class operator WString.GreaterThan( op1, op2 : WString ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue > op2.FValue; end; class operator WString.LessThanOrEqual( op1, op2 : WString ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue <= op2.FValue; end; class operator WString.GreaterThanOrEqual( op1, op2 : WString ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue >= op2.FValue; end; class operator WString.Equal( op1, op2 : WString ) : boolean; begin if op1.IsNil and op2.IsNil then Result := True else if op1.IsNil or op2.IsNil then Result := False else Result := op1.FValue = op2.FValue; end; class operator WString.NotEqual( op1, op2 : WString ) : boolean; begin if op1.IsNil and op2.IsNil then Result := False else if op1.IsNil or op2.IsNil then Result := True else Result := op1.FValue <> op2.FValue; end; function WString.replace( const oldPattern : WString ; const newPattern: WString ): WString; begin if IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := StringReplace(FValue, oldPattern , newPattern, [rfReplaceAll, rfIgnoreCase] ); end; function WString.size():WInteger; begin if IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := Length(FValue); end; function WString.isEmpty():boolean; begin if IsNil then raise EConvertError.Create(sOnlyValidOperands); if ( size <= 0 ) then Result := True else Result := False; end; function WString.quotedString(): WString; begin Result := QuotedStr( FValue ); end; function WString.lastDelimiterOf( s : String ):Integer; begin if not IsNil then Result := LastDelimiter( s, FValue ) else Result := 0; end; function WString.deleteChars( index : Integer ; n : Integer ): WString; var temp : String; begin if not IsNil then begin temp := FValue; Delete( temp , index, n); Result := temp; end else Result := ''; end; class operator WDateTime.Implicit(const Value: TDateTime): WDateTime; begin Result.FValue := Value; Result.FIsNil := False; end; class operator WDateTime.Implicit(Value: Pointer): WDateTime; begin Assert(Value = nil, sOnlyValidValueIsNil); Result.FIsNil := True; end; class operator WDateTime.Implicit(const Value: WDateTime): TDateTime; begin if Value.IsNil then raise EConvertError.Create(sCantConvertNil); Result := Value.FValue; end; class operator WDateTime.Add( op1, op2 : WDateTime ) : WDateTime; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue + op2.FValue; end; class operator WDateTime.Subtract( op1, op2 : WDateTime ) : WDateTime; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue - op2.FValue; end; class operator WDateTime.Multiply( op1, op2 : WDateTime ) : WDateTime; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue * op2.FValue; end; class operator WDateTime.Divide( op1, op2 : WDateTime ) : WDateTime; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue / op2.FValue; end; class operator WDateTime.LessThan( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue < op2.FValue; end; class operator WDateTime.GreaterThan( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue > op2.FValue; end; class operator WDateTime.LessThanOrEqual( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue <= op2.FValue; end; class operator WDateTime.GreaterThanOrEqual( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil or op2.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := op1.FValue >= op2.FValue; end; class operator WDateTime.Equal( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil and op2.IsNil then Result := True else if op1.IsNil or op2.IsNil then Result := False else Result := op1.FValue = op2.FValue; end; class operator WDateTime.NotEqual( op1, op2 : WDateTime ) : boolean; begin if op1.IsNil and op2.IsNil then Result := False else if op1.IsNil or op2.IsNil then Result := True else Result := op1.FValue <> op2.FValue; end; class operator WDateTime.Negative( op1 : WDateTime ) : WDateTime; begin if op1.IsNil then raise EConvertError.Create(sOnlyValidOperands); Result := - op1.FValue; end; function WDateTime.toStr() : String; begin if self.IsNil then Result := '' else Result := DatetoStr(self.FValue); end; function WDateTime.toStr( pattern :String ) : String; begin if self.IsNil then Result := '' else Result := FormatDateTime( pattern, FValue ); end; end.