In terms of language feature, class helpers have been around since Delphi 2005, followed by record helpers. These two constructs let you add to an existing class or record new methods, optionally also replacing existing ones. While this is more of a hack than a regular feature, it becomes important when you want to augment the features of an existing class you don't own, for example a library class. There is a caveat, though: you can have only one class helper active for each class at any time. The last visible class helper from the class scope wins over previous ones.
Fast forward to Delphi XE3. We can now write a helper for native types, and this is likely an effect of Embarcadero embracing a new compiler architecture for mobile support and preparing “future” compatibility. It might also be that Delphi is getting inspired by other languages (mostly dynamic ones, though) that provide this feature.
In any case, you can now write:
type TIntHelper = record helper for Integer function ToString: string; end;
and, given an Integer variable n, call:
How do you define that pseudo-method and how can it refer to the variable's value? By stretching the meaning of the self keyword to refer to the value the function is applied to:
function TByteHelper.ToString: string; begin Result := IntToStr (self); end;
Notice that you can apply methods also to constants, like in:
Caption := 400000.ToString;
However, you cannot do the same for a small value, as the compiler interprets constants of the smaller possible type. So if you want to get out the value 4 as a string you have to use the second form:
Caption := 4.ToString; // nope! Caption := Integer(4).ToString; // ok
Or you can make the first statement to compile by defining a different helper:
type TByteHelper = record helper for Byte...
The Delphi XE3 RTL takes advantage of this feature offering a rather complete TStringHelper, with dozens of ready-to-use functions. So that the following code will compile (if you add also the Integer helper above):
var s1: string; begin // with a variable s1 := 'Hello'; if s1.Contains('ll') then ShowMessage (s1.Substring(3)); // with a constant Left := 'Hello'.Length; // chaining Caption := ClassName.Length.ToString;