Class and Record Helpers
⚠️ Only one helper per type is active in a given scope (the nearest one wins). Your string helper hides the RTL's TStringHelper! Inherit from the existing helper to extend it: record helper (TStringHelper) for string.
type
TStringHelperEx = record helper for string
function IsValidEmail: Boolean;
function WordCount: Integer;
end;
TIntegerHelperEx = record helper for Integer
function IsEven: Boolean;
function Squared: Integer;
end;
TStringsHelper = class helper for TStrings
procedure AddFmt(const Fmt: string; const Args: array of const);
end;
function TStringHelperEx.IsValidEmail: Boolean;
begin
Result := TRegEx.IsMatch(Self, '^[^@\s]+@[^@\s]+\.[^@\s]+$');
end;
function TStringHelperEx.WordCount: Integer;
begin
Result := Length(Self.Split([' '], TStringSplitOptions.ExcludeEmpty));
end;
function TIntegerHelperEx.IsEven: Boolean; begin Result := not Odd(Self); end;
function TIntegerHelperEx.Squared: Integer; begin Result := Self * Self; end;
procedure TStringsHelper.AddFmt(const Fmt: string; const Args: array of const);
begin
Add(Format(Fmt, Args));
end;
Writeln('[email protected]'.IsValidEmail, ' ', 7.Squared, ' ', 'one two three'.WordCount);