Articles Delphi and the absolute keyword

FireWind

Завсегдатай
Staff member
Moderator
Delphi and the absolute keyword
July 16, 2020 by Jon L. Aasenden
[SHOWTOGROUPS=4,20]
There is a lot of hidden gems in the Delphi environment and compiler, and while some might regard the “absolute” keyword as obsolete, I could not disagree more; in fact I find it to be one of the most useful, flexible aspects of Delphi (and object pascal in general).

The absolute keyword allows you to define a variable of a specific type, yet instruct the compiler that it should use the memory of another variable. I cannot stress how useful this can be when used right, and how much cleaner it can make code that deal with different classes or types – that are binary compatible.

Tab pages revisited

Unlike most I try to avoid the form designer when I can. Im not purist about it, I just find that inheriting out your own controls and augmenting them results in significantly faster code, as well as a fine grained control that ordinary DFM persistence can’t always deliver.

For example: Lets say you have inherited out your own TPageControl. You have also inherited out a few TTabSheet based classes, populating the tabsheets during the constructor – so there is no design data loaded – resulting in faster display time and a more responsive UI.

In one of my events, which is called as TabSheet class is created, allowing me to prepare it, like set the icon glyph for the tab, its caption and so on – the absolute keyword makes my code faster (since there is no type-casting) and more elegant to read.

All I have to do is check for the type, and once I know which type it is, I use the variable of that type that share memory with the initial type, TTabSheet. Like this:

absolute

While only a tiny example, there are other – more demanding situations where absolute really makes a world of difference. Especially within graphics processing.

While this is just a small, miniscule example, the use of absolute can help speed up intensive code by omitting typecasts. It is also a tremendous help when dealing with low level data conversion (like dealing with 8, 15, 16, 24 and 32 bpp. When you call routines thousands of times, every bit helps – and absolute is one of those keywords that saves a few cycles per use.

Absolute is definitely one of Delphi’s un-sung heroes.
[/SHOWTOGROUPS]
 
Top