Record Helpers for Intrinsic Types

I guess that most of you already heart about it. With Delphi XE3 EMBT introduced record helpers for intrinsic types. E.g. Marco Cantu and Nick Hodges already blogged about it.

Just to explain what record helpers for intrinsic types are I will show an example.

As you can see the record helper allows you to write code that looks like calling a method of an intrinsic type instead of calling a function with the intrinsic type as an argument. This strategy is more object oriented and beautifies your code.

In Delphi XE3 EMBT already introduced some beautiful record helpers. The record helper for the string is the most important and powerful one. It has some really useful functions like Length, Equals, Format, IndexOf etc.. You can find it in the unit System.SysUtils.

Certainly you can write your own record helper for different types. You can also write a record helper for a string and write your own methods.

In the case you add your own helper unit to your uses clause and also System.SysUtils, the compiler only sees one record helper. The record helper which you add at last to your uses clause will win. Let’s have a look at an example:

With this unit you can replace the original string helper from System.SysUtils:

Instead of replacing the original helper it would be nice to derive from it but this is simply not possible. This code doesn’t compile.

Although class helpers can be derived it is not possible for record helpers. I guess it is because records also cannot be derived.

This issue means that it is nearly impossible to extend the original record helpers. IMO it must be fixed with the next Delphi version.

Anyway, currently I can imagine only one strategy to extend the original record helpers. You have to write you own helper, you have to add all functions of the original one and then you have to call the original function inside your function. And don’t forget to declare them inline because of performance. Since your helper replaces the original one you have to write a call unit for the original helper.

Let’s have a look at an example. First write the caller unit:

And then your own record helper:

Last but not least you can use your record helper:

This entry was posted in Tips and Tricks and tagged . Bookmark the permalink.